From d797a82a12872a76b950225a0a5dabd5e988bce0 Mon Sep 17 00:00:00 2001 From: Tomas Della Vedova Date: Thu, 25 Mar 2021 09:47:16 +0100 Subject: [PATCH] ES client : use the new type definitions (#83808) * Use client from branch * Get type checking working in core * Fix types in other plugins * Update client types + remove type errors from core * migrate Task Manager Elasticsearch typing from legacy library to client library * use SortOrder instead o string in alerts * Update client types + fix core type issues * fix maps ts errors * Update Lens types * Convert Search Profiler body from a string to an object to conform to SearchRequest type. * Fix SOT types * Fix/mute Security/Spaces plugins type errors. * Fix bootstrap types * Fix painless_lab * corrected es typing in Event Log * Use new types from client for inferred search responses * Latest type defs * Integrate latest type defs for APM/UX * fix core errors * fix telemetry errors * fix canvas errors * fix data_enhanced errors * fix event_log errors * mute lens errors * fix or mute maps errors * fix reporting errors * fix security errors * mute errors in task_manager * fix errors in telemetry_collection_xpack * fix errors in data plugins * fix errors in alerts * mute errors in index_management * fix task_manager errors * mute or fix lens errors * fix upgrade_assistant errors * fix or mute errors in index_lifecycle_management * fix discover errors * fix core tests * ML changes * fix core type errors * mute error in kbn-es-archiver * fix error in data plugin * fix error in telemetry plugin * fix error in discover * fix discover errors * fix errors in task_manager * fix security errors * fix wrong conflict resolution * address errors with upstream code * update deps to the last commit * remove outdated comments * fix core errors * fix errors after update * adding more expect errors to ML * pull the lastest changes * fix core errors * fix errors in infra plugin * fix errors in uptime plugin * fix errors in ml * fix errors in xpack telemetry * fix or mute errors in transform * fix errors in upgrade assistant * fix or mute fleet errors * start fixing apm errors * fix errors in osquery * fix telemetry tests * core cleanup * fix asMutableArray imports * cleanup * data_enhanced cleanup * cleanup events_log * cleaup * fix error in kbn-es-archiver * fix errors in kbn-es-archiver * fix errors in kbn-es-archiver * fix ES typings for Hit * fix SO * fix actions plugin * fix fleet * fix maps * fix stack_alerts * fix eslint problems * fix event_log unit tests * fix failures in data_enhanced tests * fix test failure in kbn-es-archiver * fix test failures in index_pattern_management * fixing ML test * remove outdated comment in kbn-es-archiver * fix error type in ml * fix eslint errors in osquery plugin * fix runtime error in infra plugin * revert changes to event_log cluser exist check * fix eslint error in osquery * fixing ML endpoint argument types * fx types * Update api-extractor docs * attempt fix for ese test * Fix lint error * Fix types for ts refs * Fix data_enhanced unit test * fix lens types * generate docs * Fix a number of type issues in monitoring and ml * fix triggers_actions_ui * Fix ILM functional test * Put search.d.ts typings back * fix data plugin * Update typings in typings/elasticsearch * Update snapshots * mute errors in task_manager * mute fleet errors * lens. remove unnecessary ts-expect-errors * fix errors in stack_alerts * mute errors in osquery * fix errors in security_solution * fix errors in lists * fix errors in cases * mute errors in search_examples * use KibanaClient to enforce promise-based API * fix errors in test/ folder * update comment * fix errors in x-pack/test folder * fix errors in ml plugin * fix optional fields in ml api_integartoon tests * fix another casting problem in ml tests * fix another ml test failure * fix fleet problem after conflict resolution * rollback changes in security_solution. trying to fix test * Update type for discover rows * uncomment runtime_mappings as its outdated * address comments from Wylie * remove eslint error due to any * mute error due to incompatibility * Apply suggestions from code review Co-authored-by: John Schulz * fix type error in lens tests * Update x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts Co-authored-by: Alison Goryachev * Update x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts Co-authored-by: Alison Goryachev * update deps * fix errors in core types * fix errors for the new elastic/elasticsearch version * remove unused type * remove unnecessary manual type cast and put optional chaining back * ML: mute Datafeed is missing indices_options * Apply suggestions from code review Co-authored-by: Josh Dover <1813008+joshdover@users.noreply.github.com> * use canary pacakge instead of git commit Co-authored-by: Josh Dover Co-authored-by: Josh Dover <1813008+joshdover@users.noreply.github.com> Co-authored-by: Gidi Meir Morris Co-authored-by: Nathan Reese Co-authored-by: Wylie Conlon Co-authored-by: CJ Cenizal Co-authored-by: Aleh Zasypkin Co-authored-by: Dario Gieselaar Co-authored-by: restrry Co-authored-by: James Gowdy Co-authored-by: John Schulz Co-authored-by: Alison Goryachev # Conflicts: # package.json # src/core/server/saved_objects/migrationsv2/integration_tests/migration.test.ts # src/plugins/data/server/autocomplete/value_suggestions_route.ts # src/plugins/discover/public/application/angular/doc_table/create_doc_table_react.tsx # x-pack/plugins/apm/server/lib/services/get_service_dependencies/get_destination_map.ts # x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts # x-pack/plugins/security/server/authentication/api_keys/api_keys.ts # yarn.lock --- ...gin-core-public.savedobjectsfindoptions.md | 4 +- ...lic.savedobjectsfindoptions.searchafter.md | 2 +- ...ublic.savedobjectsfindoptions.sortorder.md | 2 +- ...gin-core-server.savedobjectsfindoptions.md | 4 +- ...ver.savedobjectsfindoptions.searchafter.md | 2 +- ...erver.savedobjectsfindoptions.sortorder.md | 2 +- ...ugin-core-server.savedobjectsfindresult.md | 2 +- ...core-server.savedobjectsfindresult.sort.md | 2 +- ...n-plugins-data-public.iessearchresponse.md | 2 +- ...-plugins-data-public.searchsource.fetch.md | 4 +- ...n-plugins-data-server.iessearchresponse.md | 2 +- .../search_examples/public/search/app.tsx | 3 +- .../public/search_sessions/app.tsx | 4 +- package.json | 2 +- .../src/actions/empty_kibana_index.ts | 6 +- packages/kbn-es-archiver/src/actions/load.ts | 6 +- packages/kbn-es-archiver/src/actions/save.ts | 4 +- .../kbn-es-archiver/src/actions/unload.ts | 4 +- .../kbn-es-archiver/src/client_headers.ts | 2 +- packages/kbn-es-archiver/src/es_archiver.ts | 6 +- .../lib/docs/generate_doc_records_stream.ts | 4 +- .../src/lib/docs/index_doc_records_stream.ts | 4 +- .../src/lib/indices/__mocks__/stubs.ts | 4 +- .../lib/indices/create_index_stream.test.ts | 1 - .../src/lib/indices/create_index_stream.ts | 11 +- .../src/lib/indices/delete_index.ts | 6 +- .../src/lib/indices/delete_index_stream.ts | 4 +- .../indices/generate_index_records_stream.ts | 4 +- .../src/lib/indices/kibana_index.ts | 36 +- src/core/public/public.api.md | 5 +- .../core_usage_data_service.test.ts | 16 +- .../core_usage_data_service.ts | 12 +- src/core/server/elasticsearch/client/mocks.ts | 6 +- .../client/retry_call_cluster.test.ts | 6 +- .../export/saved_objects_exporter.test.ts | 7 +- .../import/lib/check_origin_conflicts.ts | 2 +- .../server/saved_objects/mappings/types.ts | 2 +- .../core/build_active_mappings.test.ts | 1 + .../migrations/core/call_cluster.ts | 14 +- .../migrations/core/elastic_index.test.ts | 72 +-- .../migrations/core/elastic_index.ts | 15 +- .../migrations/core/index_migrator.test.ts | 16 +- .../migrations/core/index_migrator.ts | 12 +- .../migrations/kibana/kibana_migrator.test.ts | 26 +- .../migrationsv2/actions/index.ts | 93 ++- .../integration_tests/actions.test.ts | 6 +- .../integration_tests/migration.test.ts | 4 +- .../saved_objects/migrationsv2/model.ts | 1 - .../service/lib/included_fields.ts | 5 +- .../service/lib/point_in_time_finder.ts | 14 +- .../service/lib/repository.test.js | 27 +- .../saved_objects/service/lib/repository.ts | 169 +++-- .../service/lib/search_dsl/search_dsl.test.ts | 10 +- .../service/lib/search_dsl/search_dsl.ts | 7 +- .../service/lib/search_dsl/sorting_params.ts | 5 +- .../service/saved_objects_client.ts | 2 +- src/core/server/saved_objects/types.ts | 5 +- .../version/encode_hit_version.ts | 2 +- .../saved_objects/version/encode_version.ts | 2 +- src/core/server/server.api.md | 7 +- .../data/common/search/es_search/types.ts | 9 +- .../utils/courier_inspector_stats.ts | 4 +- .../search_source/fetch/request_error.ts | 6 +- .../search/search_source/fetch/types.ts | 7 +- .../search_source/legacy/call_client.ts | 4 +- .../search/search_source/legacy/fetch_soon.ts | 8 +- .../search/search_source/legacy/types.ts | 7 +- src/plugins/data/public/public.api.md | 9 +- .../search/expressions/es_raw_response.ts | 8 +- .../public/search/fetch/handle_response.tsx | 4 +- .../shard_failure_modal.tsx | 4 +- .../shard_failure_open_modal_button.tsx | 4 +- .../autocomplete/value_suggestions_route.ts | 7 +- .../index_patterns/fetcher/lib/es_api.ts | 13 +- .../field_capabilities/field_caps_response.ts | 18 +- .../fetcher/lib/field_capabilities/index.ts | 1 - .../fetcher/lib/resolve_time_pattern.ts | 4 +- .../data/server/search/collectors/fetch.ts | 6 +- .../search/es_search/es_search_strategy.ts | 3 +- .../server/search/es_search/response_utils.ts | 8 +- .../data/server/search/routes/call_msearch.ts | 6 +- src/plugins/data/server/server.api.md | 42 +- .../doc_table/create_doc_table_react.tsx | 3 +- .../context_app/context_app_legacy.tsx | 1 + .../discover_grid/discover_grid_flyout.tsx | 2 +- .../get_render_cell_value.test.tsx | 21 +- .../components/table/table.test.tsx | 4 +- .../application/doc_views/doc_views_types.ts | 4 +- .../embeddable/search_embeddable.ts | 2 +- src/plugins/embeddable/public/public.api.md | 1 + .../lib/get_indices.ts | 1 + .../routes/preview_scripted_field.test.ts | 20 +- .../server/routes/preview_scripted_field.ts | 6 +- .../kibana/get_saved_object_counts.ts | 1 + .../server/check_cluster_data.test.ts | 4 + .../security_oss/server/check_cluster_data.ts | 8 +- .../telemetry_collection/get_cluster_info.ts | 18 +- .../get_data_telemetry/get_data_telemetry.ts | 8 +- .../get_local_stats.test.ts | 49 +- .../telemetry_collection/get_local_stats.ts | 5 +- .../telemetry_collection/get_nodes_usage.ts | 5 +- .../vis_type_vega/public/data_model/types.ts | 6 +- .../usage_collector/get_usage_collector.ts | 6 +- .../usage_collector/get_usage_collector.ts | 2 +- test/api_integration/apis/home/sample_data.ts | 8 +- .../apis/saved_objects/migrations.ts | 8 +- .../telemetry/telemetry_optin_notice_seen.ts | 8 +- .../apis/ui_metric/ui_metric.ts | 4 +- test/common/services/elasticsearch.ts | 6 +- typings/elasticsearch/aggregations.d.ts | 466 -------------- typings/elasticsearch/index.d.ts | 128 +--- typings/elasticsearch/search.d.ts | 577 ++++++++++++++++++ .../actions/server/actions_client.test.ts | 6 + .../plugins/actions/server/actions_client.ts | 4 +- .../server/builtin_action_types/es_index.ts | 4 +- .../server/usage/actions_telemetry.test.ts | 2 + .../actions/server/usage/actions_telemetry.ts | 22 +- .../server/alerts_client/alerts_client.ts | 3 +- .../server/usage/alerts_telemetry.test.ts | 1 + .../alerting/server/usage/alerts_telemetry.ts | 58 +- .../VisitorBreakdownMap/EmbeddedMap.test.tsx | 2 +- .../create-functional-tests-archive/index.ts | 3 +- .../scripts/upload-telemetry-data/index.ts | 10 +- .../server/lib/alerts/alerting_es_client.ts | 4 +- .../chart_preview/get_transaction_duration.ts | 6 +- .../register_error_count_alert_type.test.ts | 30 + ..._transaction_error_rate_alert_type.test.ts | 40 ++ .../collect_data_telemetry/index.ts | 4 +- .../collect_data_telemetry/tasks.ts | 15 +- .../apm/server/lib/apm_telemetry/index.ts | 2 +- .../index.ts | 2 +- .../get_latency_distribution.ts | 2 +- .../index.ts | 6 +- .../process_significant_term_aggs.ts | 2 +- .../lib/errors/get_error_group_sample.ts | 5 +- .../apm/server/lib/errors/get_error_groups.ts | 3 +- .../create_apm_event_client/index.ts | 4 +- .../create_internal_es_client/index.ts | 12 +- .../server/lib/helpers/setup_request.test.ts | 12 +- .../lib/helpers/transaction_error_rate.ts | 2 +- .../java/gc/fetch_and_transform_gc_metrics.ts | 8 +- .../rum_client/get_page_load_distribution.ts | 16 +- .../lib/rum_client/get_pl_dist_breakdown.ts | 6 +- .../lib/rum_client/get_web_core_vitals.ts | 2 +- .../lib/service_map/get_service_anomalies.ts | 7 +- .../lib/service_map/get_trace_sample_ids.ts | 5 +- .../__snapshots__/queries.test.ts.snap | 16 +- .../annotations/get_stored_annotations.ts | 8 +- .../get_destination_map.ts | 13 +- .../get_service_error_groups/index.ts | 4 +- .../services/get_service_metadata_details.ts | 3 +- .../get_service_transaction_stats.ts | 4 +- .../get_services_from_metric_documents.ts | 2 +- .../convert_settings_to_string.ts | 4 +- .../create_agent_config_index.ts | 4 +- .../find_exact_configuration.ts | 6 +- .../search_configurations.ts | 6 +- .../custom_link/create_custom_link_index.ts | 8 +- .../settings/custom_link/get_transaction.ts | 19 +- .../settings/custom_link/list_custom_links.ts | 5 +- .../apm/server/lib/traces/get_trace_items.ts | 3 +- .../__snapshots__/queries.test.ts.snap | 16 +- .../server/lib/transaction_groups/fetcher.ts | 5 +- .../get_transaction_group_stats.ts | 16 +- .../distribution/get_buckets/index.ts | 10 +- .../transactions/get_anomaly_data/fetcher.ts | 8 +- .../lib/transactions/get_transaction/index.ts | 5 +- .../plugins/apm/server/projections/metrics.ts | 3 +- .../projections/rum_page_load_transactions.ts | 8 +- .../apm/server/projections/transactions.ts | 3 +- .../plugins/apm/server/projections/typings.ts | 15 +- .../util/merge_projection/index.test.ts | 6 +- .../util/merge_projection/index.ts | 10 +- .../collectors/custom_element_collector.ts | 7 +- .../server/collectors/workpad_collector.ts | 6 +- .../cases/server/services/alerts/index.ts | 3 +- .../data_enhanced/server/collectors/fetch.ts | 3 +- .../data_enhanced/server/routes/session.ts | 2 +- .../server/search/eql_search_strategy.ts | 11 +- .../server/search/es_search_strategy.test.ts | 2 +- .../server/search/es_search_strategy.ts | 13 +- .../search/session/get_search_status.ts | 1 + .../data_enhanced/server/search/types.ts | 3 +- .../server/es/cluster_client_adapter.test.ts | 92 ++- .../server/es/cluster_client_adapter.ts | 129 ++-- .../file_upload/server/analyze_file.tsx | 13 +- .../server/routes/data_streams/handlers.ts | 1 + .../fleet/server/services/agents/actions.ts | 2 +- .../fleet/server/services/agents/crud.ts | 28 +- .../fleet/server/services/agents/helpers.ts | 15 +- .../server/services/agents/reassign.test.ts | 4 +- .../server/services/agents/status.test.ts | 8 +- .../server/services/agents/unenroll.test.ts | 6 +- .../services/api_keys/enrollment_api_key.ts | 9 +- .../server/services/artifacts/artifacts.ts | 10 +- .../server/services/artifacts/mappings.ts | 4 +- .../fleet/server/services/artifacts/mocks.ts | 4 +- .../epm/elasticsearch/template/template.ts | 1 + .../epm/elasticsearch/transform/install.ts | 1 + .../epm/elasticsearch/transform/remove.ts | 1 + .../fleet_server/saved_object_migrations.ts | 1 + .../api/index/register_add_policy_route.ts | 1 + .../api/policies/register_delete_route.ts | 1 + .../api/policies/register_fetch_route.ts | 1 + .../templates/register_add_policy_route.ts | 10 +- .../api/data_streams/register_get_route.ts | 4 +- .../public/utils/logs_overview_fetchers.ts | 6 +- .../framework/kibana_framework_adapter.ts | 4 +- .../evaluate_condition.ts | 1 + .../metric_threshold/lib/evaluate_alert.ts | 8 +- .../log_entries_search_strategy.ts | 1 + .../log_entries/log_entry_search_strategy.ts | 1 + .../log_entries/queries/log_entries.ts | 5 +- .../services/log_entries/queries/log_entry.ts | 5 +- .../server/routes/existing_fields.test.ts | 10 +- .../lens/server/routes/existing_fields.ts | 18 +- .../plugins/lens/server/routes/field_stats.ts | 17 +- x-pack/plugins/lens/server/usage/task.ts | 3 + .../lens/server/usage/visualization_counts.ts | 1 + .../services/items/create_list_item.test.ts | 2 + .../server/services/items/create_list_item.ts | 3 +- .../services/items/find_list_item.test.ts | 2 + .../server/services/items/find_list_item.ts | 7 +- .../server/services/items/get_list_item.ts | 4 +- .../services/items/update_list_item.test.ts | 1 + .../server/services/items/update_list_item.ts | 3 +- .../items/write_list_items_to_stream.test.ts | 2 + .../items/write_list_items_to_stream.ts | 12 +- .../server/services/lists/create_list.test.ts | 3 + .../server/services/lists/create_list.ts | 3 +- .../lists/server/services/lists/find_list.ts | 5 +- .../lists/server/services/lists/get_list.ts | 3 +- .../server/services/lists/update_list.test.ts | 2 + .../server/services/lists/update_list.ts | 3 +- .../services/utils/get_search_after_scroll.ts | 4 +- .../get_search_after_with_tie_breaker.ts | 15 +- .../utils/get_sort_with_tie_breaker.ts | 14 +- ...sform_elastic_named_search_to_list_item.ts | 4 +- .../utils/transform_elastic_to_list.ts | 5 +- .../utils/transform_elastic_to_list_item.ts | 35 +- .../classes/sources/es_source/es_source.ts | 22 +- x-pack/plugins/maps/server/mvt/get_tile.ts | 2 + .../types/anomaly_detection_jobs/datafeed.ts | 69 ++- .../types/anomaly_detection_jobs/job.ts | 141 +++-- x-pack/plugins/ml/common/types/fields.ts | 3 +- x-pack/plugins/ml/common/util/job_utils.ts | 4 +- .../plugins/ml/common/util/parse_interval.ts | 5 +- .../components/data_grid/common.ts | 1 + .../chart_loader.ts | 1 + .../job_creator/advanced_job_creator.ts | 10 +- .../new_job/common/job_creator/job_creator.ts | 16 +- .../job_creator/util/default_configs.ts | 2 + .../util/filter_runtime_mappings.test.ts | 3 + .../categorization_examples_loader.ts | 1 + .../common/results_loader/results_loader.ts | 1 + .../advanced_detector_modal.tsx | 6 +- .../estimate_bucket_span.ts | 1 + .../metric_selection_summary.tsx | 1 + .../multi_metric_view/metric_selection.tsx | 2 + .../metric_selection_summary.tsx | 2 + .../population_view/metric_selection.tsx | 2 + .../metric_selection_summary.tsx | 2 + .../single_metric_view/metric_selection.tsx | 1 + .../metric_selection_summary.tsx | 1 + .../components/time_range_step/time_range.tsx | 1 + .../public/application/util/string_utils.ts | 2 +- .../ml/server/lib/alerts/alerting_service.ts | 4 +- .../ml/server/lib/ml_client/ml_client.ts | 32 +- .../plugins/ml/server/lib/ml_client/search.ts | 9 +- x-pack/plugins/ml/server/lib/node_utils.ts | 2 +- x-pack/plugins/ml/server/lib/query_utils.ts | 6 +- .../models/annotation_service/annotation.ts | 2 + .../calculate_model_memory_limit.ts | 11 +- .../models/calendar/calendar_manager.ts | 18 +- .../server/models/calendar/event_manager.ts | 13 +- .../ml/server/models/calendar/index.ts | 1 - .../analytics_audit_messages.ts | 6 +- .../models/data_frame_analytics/validation.ts | 5 +- .../models/data_recognizer/data_recognizer.ts | 5 +- .../models/data_visualizer/data_visualizer.ts | 3 + .../models/fields_service/fields_service.ts | 8 + .../ml/server/models/filter/filter_manager.ts | 56 +- .../ml/server/models/job_service/datafeeds.ts | 26 +- .../ml/server/models/job_service/jobs.ts | 26 +- .../models/job_service/model_snapshots.ts | 11 +- .../new_job/categorization/examples.ts | 28 +- .../new_job/categorization/top_categories.ts | 2 + .../job_service/new_job_caps/field_service.ts | 11 +- .../models/job_service/new_job_caps/rollup.ts | 5 +- .../models/job_validation/job_validation.ts | 1 + .../validate_model_memory_limit.test.ts | 51 +- .../get_partition_fields_values.ts | 3 +- .../models/results_service/results_service.ts | 5 + .../ml/server/routes/anomaly_detectors.ts | 28 +- .../ml/server/routes/data_frame_analytics.ts | 9 +- x-pack/plugins/ml/server/routes/datafeeds.ts | 18 +- .../ml/server/routes/trained_models.ts | 5 +- .../plugins/ml/server/saved_objects/checks.ts | 16 +- .../shared_services/providers/system.ts | 11 +- .../lib/alerts/fetch_available_ccs.test.ts | 5 +- .../lib/alerts/fetch_ccr_read_exceptions.ts | 5 +- .../lib/alerts/fetch_cluster_health.test.ts | 3 +- .../server/lib/alerts/fetch_cluster_health.ts | 12 +- .../server/lib/alerts/fetch_clusters.test.ts | 5 +- .../alerts/fetch_cpu_usage_node_stats.test.ts | 8 +- .../lib/alerts/fetch_cpu_usage_node_stats.ts | 4 +- .../fetch_disk_usage_node_stats.test.ts | 1 + .../lib/alerts/fetch_disk_usage_node_stats.ts | 3 +- .../fetch_elasticsearch_versions.test.ts | 3 +- .../alerts/fetch_elasticsearch_versions.ts | 14 +- .../lib/alerts/fetch_index_shard_size.ts | 5 +- .../lib/alerts/fetch_kibana_versions.test.ts | 1 + .../lib/alerts/fetch_kibana_versions.ts | 2 +- .../server/lib/alerts/fetch_licenses.test.ts | 3 +- .../server/lib/alerts/fetch_licenses.ts | 12 +- .../alerts/fetch_logstash_versions.test.ts | 1 + .../lib/alerts/fetch_logstash_versions.ts | 2 +- .../alerts/fetch_memory_usage_node_stats.ts | 1 + .../fetch_missing_monitoring_data.test.ts | 2 + .../alerts/fetch_missing_monitoring_data.ts | 4 +- .../alerts/fetch_nodes_from_cluster_stats.ts | 9 +- .../fetch_thread_pool_rejections_stats.ts | 9 +- x-pack/plugins/observability/server/index.ts | 4 +- .../annotations/create_annotations_client.ts | 29 +- .../server/lib/annotations/mappings.ts | 4 +- .../server/utils/create_or_update_index.ts | 31 +- .../search_strategy/osquery/actions/index.ts | 6 +- .../search_strategy/osquery/results/index.ts | 4 +- .../action_results/action_results_table.tsx | 9 +- .../osquery/public/actions/actions_table.tsx | 1 + .../osquery/public/results/results_table.tsx | 2 + .../osquery/factory/actions/all/index.ts | 1 + .../osquery/factory/actions/results/index.ts | 1 + .../osquery/factory/agents/index.ts | 6 +- .../osquery/factory/results/index.ts | 1 + .../painless_lab/server/routes/api/execute.ts | 1 + .../generate_csv/generate_csv.ts | 2 +- .../server/usage/get_reporting_usage.ts | 9 +- .../server/usage/fetch_tag_usage_data.ts | 32 +- .../searchprofiler/server/routes/profile.ts | 13 +- .../common/model/authenticated_user.mock.ts | 6 +- .../authentication/api_keys/api_keys.test.ts | 27 +- .../authentication/api_keys/api_keys.ts | 33 +- .../server/authentication/providers/base.ts | 3 +- .../authentication/providers/kerberos.test.ts | 4 +- .../authentication/providers/kerberos.ts | 8 +- .../authentication/providers/oidc.test.ts | 3 +- .../authentication/providers/saml.test.ts | 3 +- .../authentication/providers/token.test.ts | 1 + .../server/authentication/providers/token.ts | 17 +- .../server/authentication/tokens.test.ts | 39 +- .../security/server/authentication/tokens.ts | 18 +- .../authorization/check_privileges.test.ts | 18 +- .../server/authorization/check_privileges.ts | 32 +- .../authorization/privileges/get_builtin.ts | 3 +- .../server/routes/authorization/roles/get.ts | 8 +- .../routes/authorization/roles/get_all.ts | 7 +- .../roles/model/elasticsearch_role.ts | 2 +- .../server/routes/authorization/roles/put.ts | 10 +- .../server/routes/indices/get_fields.ts | 36 +- .../server/routes/role_mapping/get.ts | 9 +- .../security/server/routes/users/get.ts | 4 +- .../session_management/session_index.test.ts | 70 ++- .../session_management/session_index.ts | 17 +- .../common/search_strategy/common/index.ts | 7 +- .../security_solution/index.ts | 12 +- .../public/common/containers/source/index.tsx | 2 +- .../server/endpoint/routes/resolver/entity.ts | 8 +- .../routes/resolver/queries/events.ts | 11 +- .../resolver/tree/queries/descendants.ts | 6 +- .../routes/resolver/tree/queries/lifecycle.ts | 5 +- .../routes/resolver/tree/queries/stats.ts | 5 +- .../index/delete_all_index.ts | 1 + .../index/get_index_exists.test.ts | 2 + .../index/get_index_exists.ts | 4 +- .../migrations/create_migration.ts | 2 +- .../get_index_versions_by_index.test.ts | 2 +- .../get_signal_versions_by_index.test.ts | 2 +- .../get_signal_versions_by_index.ts | 5 +- .../get_signals_indices_in_range.ts | 7 +- .../notifications/get_signals.ts | 4 +- .../rules_notification_alert_type.ts | 1 + .../routes/index/get_index_version.ts | 13 +- .../signals/__mocks__/es_results.ts | 4 +- .../signals/build_bulk_body.test.ts | 12 + .../signals/build_bulk_body.ts | 4 +- .../signals/build_event_type_signal.test.ts | 3 + .../signals/build_event_type_signal.ts | 7 +- .../signals/build_events_query.ts | 12 +- .../signals/build_rule.test.ts | 6 + .../detection_engine/signals/build_rule.ts | 3 + .../signals/build_signal.test.ts | 8 + .../detection_engine/signals/build_signal.ts | 16 +- .../signals/bulk_create_ml_signals.ts | 13 +- .../signals/filters/filter_events.ts | 5 +- .../filters/filter_events_against_list.ts | 11 +- .../detection_engine/signals/filters/types.ts | 11 +- .../build_risk_score_from_mapping.test.ts | 1 + .../build_rule_name_from_mapping.test.ts | 1 + .../build_severity_from_mapping.test.ts | 1 + .../signals/search_after_bulk_create.test.ts | 17 + .../signals/search_after_bulk_create.ts | 4 + .../signals/send_telemetry_events.ts | 3 +- .../signals/signal_rule_alert_type.test.ts | 6 +- .../signals/single_bulk_create.test.ts | 3 + .../signals/single_bulk_create.ts | 12 +- .../signals/single_search_after.test.ts | 5 +- .../signals/single_search_after.ts | 12 +- .../build_threat_mapping_filter.mock.ts | 5 +- .../threat_mapping/create_threat_signals.ts | 1 + .../enrich_signal_threat_matches.ts | 2 +- .../signals/threat_mapping/get_threat_list.ts | 17 +- .../signals/threat_mapping/types.ts | 6 +- .../bulk_create_threshold_signals.ts | 8 +- .../threshold/find_threshold_signals.ts | 5 +- .../threshold/get_threshold_bucket_filters.ts | 2 +- .../lib/detection_engine/signals/types.ts | 10 +- .../detection_engine/signals/utils.test.ts | 14 +- .../lib/detection_engine/signals/utils.ts | 16 +- .../server/lib/machine_learning/index.ts | 11 +- .../factory/hosts/all/query.all_hosts.dsl.ts | 2 +- .../hosts/authentications/dsl/query.dsl.ts | 16 +- .../query.hosts_kpi_authentications.dsl.ts | 4 +- .../kpi/hosts/query.hosts_kpi_hosts.dsl.ts | 2 +- .../query.hosts_kpi_unique_ips.dsl.ts | 4 +- .../factory/hosts/overview/index.ts | 1 + .../hosts/overview/query.overview_host.dsl.ts | 3 +- .../hosts/uncommon_processes/dsl/query.dsl.ts | 20 +- .../factory/matrix_histogram/index.ts | 1 + .../network/details/__mocks__/index.ts | 20 +- .../details/query.details_network.dsl.ts | 6 +- .../factory/network/dns/index.ts | 1 + .../factory/network/http/index.ts | 1 + .../factory/network/kpi/dns/index.ts | 1 + .../network/kpi/network_events/index.ts | 1 + .../network/kpi/tls_handshakes/index.ts | 1 + .../network/kpi/unique_private_ips/index.ts | 1 + .../factory/network/overview/index.ts | 1 + .../timeline/factory/events/all/index.ts | 1 + .../timeline/factory/events/details/index.ts | 1 + .../usage/detections/detections_helpers.ts | 10 +- .../spaces_usage_collector.ts | 1 + .../common/build_sorted_events_query.ts | 15 +- .../alert_types/es_query/expression.tsx | 8 +- .../alert_types/es_query/action_context.ts | 4 +- .../alert_types/es_query/alert_type.test.ts | 8 + .../server/alert_types/es_query/alert_type.ts | 10 +- .../geo_containment/es_query_builder.ts | 7 +- .../geo_containment/geo_containment.ts | 8 +- .../monitoring/workload_statistics.test.ts | 167 ++--- .../server/monitoring/workload_statistics.ts | 102 +++- .../mark_available_tasks_as_claimed.test.ts | 39 +- .../mark_available_tasks_as_claimed.ts | 37 +- .../server/queries/query_clauses.test.ts | 21 +- .../server/queries/query_clauses.ts | 214 +------ .../server/queries/task_claiming.test.ts | 8 +- .../server/queries/task_claiming.ts | 11 +- .../task_manager/server/task_store.test.ts | 23 +- .../plugins/task_manager/server/task_store.ts | 44 +- .../telemetry_collection/get_license.ts | 21 +- .../get_stats_with_xpack.test.ts | 5 +- .../server/routes/api/field_histograms.ts | 1 + .../transform/server/routes/api/privileges.ts | 5 +- .../transform/server/routes/api/transforms.ts | 10 +- .../routes/api/transforms_audit_messages.ts | 21 +- .../server/data/lib/time_series_query.ts | 9 +- .../server/data/routes/indices.ts | 13 +- .../server/lib/es_indices_state_check.ts | 4 +- .../server/lib/es_migration_apis.test.ts | 4 + .../server/lib/es_migration_apis.ts | 4 +- .../lib/reindexing/reindex_actions.test.ts | 1 + .../lib/reindexing/reindex_service.test.ts | 25 +- .../server/lib/reindexing/reindex_service.ts | 19 +- .../uptime/common/utils/as_mutable_array.ts | 41 ++ x-pack/plugins/uptime/server/lib/lib.ts | 6 +- .../uptime/server/lib/requests/get_certs.ts | 6 +- .../lib/requests/get_journey_details.ts | 9 +- .../lib/requests/get_journey_failed_steps.ts | 12 +- .../lib/requests/get_journey_screenshot.ts | 3 +- .../server/lib/requests/get_journey_steps.ts | 20 +- .../lib/requests/get_last_successful_step.ts | 3 +- .../server/lib/requests/get_latest_monitor.ts | 7 +- .../lib/requests/get_monitor_availability.ts | 10 +- .../lib/requests/get_monitor_details.ts | 5 +- .../lib/requests/get_monitor_duration.ts | 3 +- .../lib/requests/get_monitor_locations.ts | 5 +- .../server/lib/requests/get_monitor_states.ts | 2 +- .../server/lib/requests/get_monitor_status.ts | 8 +- .../server/lib/requests/get_network_events.ts | 3 +- .../server/lib/requests/get_ping_histogram.ts | 4 +- .../uptime/server/lib/requests/get_pings.ts | 5 +- .../uptime/server/lib/requests/helper.ts | 3 +- .../requests/search/find_potential_matches.ts | 9 +- .../apps/index_lifecycle_management.ts | 1 + x-pack/test/accessibility/apps/ml.ts | 5 +- .../api_integration/apis/es/has_privileges.ts | 1 + .../api_integration/apis/lens/telemetry.ts | 5 +- .../apis/ml/annotations/create_annotations.ts | 1 + .../apis/ml/annotations/delete_annotations.ts | 1 + .../apis/ml/annotations/get_annotations.ts | 1 + .../apis/ml/annotations/update_annotations.ts | 9 +- .../apis/ml/anomaly_detectors/get.ts | 1 + .../apis/ml/calendars/create_calendars.ts | 6 +- .../apis/ml/calendars/delete_calendars.ts | 1 + .../apis/ml/calendars/get_calendars.ts | 6 + .../apis/ml/calendars/helpers.ts | 9 +- .../apis/ml/calendars/update_calendars.ts | 2 + .../apis/ml/jobs/common_jobs.ts | 3 + .../ml/results/get_anomalies_table_data.ts | 2 + .../apis/ml/results/get_categorizer_stats.ts | 2 + .../apis/ml/results/get_stopped_partitions.ts | 2 + .../api_integration/apis/security/roles.ts | 1 + .../apis/telemetry/telemetry_local.ts | 2 +- .../telemetry/telemetry_optin_notice_seen.ts | 8 +- .../basic/tests/cases/patch_cases.ts | 26 +- .../tests/cases/sub_cases/patch_sub_cases.ts | 36 +- .../case_api_integration/common/lib/utils.ts | 52 +- .../tests/create_signals_migrations.ts | 1 + .../detection_engine_api_integration/utils.ts | 13 +- .../fleet_api_integration/apis/agents/acks.ts | 5 +- .../apis/agents/checkin.ts | 1 + .../apis/agents/unenroll.ts | 4 +- .../fleet_api_integration/apis/fleet_setup.ts | 1 + .../aggregated_scripted_job.ts | 4 + .../apps/ml/anomaly_detection/annotations.ts | 2 + .../ml/anomaly_detection/anomaly_explorer.ts | 2 + .../anomaly_detection/single_metric_viewer.ts | 4 + .../apps/ml/permissions/full_ml_access.ts | 5 +- .../apps/ml/permissions/read_ml_access.ts | 5 +- .../apps/ml/settings/calendar_creation.ts | 1 + .../apps/ml/settings/calendar_edit.ts | 2 + x-pack/test/functional/services/ml/api.ts | 85 ++- .../functional/services/ml/common_config.ts | 3 + .../apps/ml/alert_flyout.ts | 2 + x-pack/test/lists_api_integration/utils.ts | 5 +- .../trial/tests/annotations.ts | 11 +- .../common/lib/create_users_and_roles.ts | 6 +- .../tests/session_idle/cleanup.ts | 6 +- .../tests/session_lifespan/cleanup.ts | 6 +- .../apis/package.ts | 21 +- .../services/resolver.ts | 13 +- yarn.lock | 14 +- 542 files changed, 3652 insertions(+), 3050 deletions(-) delete mode 100644 typings/elasticsearch/aggregations.d.ts create mode 100644 typings/elasticsearch/search.d.ts create mode 100644 x-pack/plugins/uptime/common/utils/as_mutable_array.ts diff --git a/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.md b/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.md index 69cfb818561e5d..7be45c6c173b45 100644 --- a/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.md +++ b/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.md @@ -27,10 +27,10 @@ export interface SavedObjectsFindOptions | [preference](./kibana-plugin-core-public.savedobjectsfindoptions.preference.md) | string | An optional ES preference value to be used for the query \* | | [rootSearchFields](./kibana-plugin-core-public.savedobjectsfindoptions.rootsearchfields.md) | string[] | The fields to perform the parsed query against. Unlike the searchFields argument, these are expected to be root fields and will not be modified. If used in conjunction with searchFields, both are concatenated together. | | [search](./kibana-plugin-core-public.savedobjectsfindoptions.search.md) | string | Search documents using the Elasticsearch Simple Query String syntax. See Elasticsearch Simple Query String query argument for more information | -| [searchAfter](./kibana-plugin-core-public.savedobjectsfindoptions.searchafter.md) | unknown[] | Use the sort values from the previous page to retrieve the next page of results. | +| [searchAfter](./kibana-plugin-core-public.savedobjectsfindoptions.searchafter.md) | estypes.Id[] | Use the sort values from the previous page to retrieve the next page of results. | | [searchFields](./kibana-plugin-core-public.savedobjectsfindoptions.searchfields.md) | string[] | The fields to perform the parsed query against. See Elasticsearch Simple Query String fields argument for more information | | [sortField](./kibana-plugin-core-public.savedobjectsfindoptions.sortfield.md) | string | | -| [sortOrder](./kibana-plugin-core-public.savedobjectsfindoptions.sortorder.md) | string | | +| [sortOrder](./kibana-plugin-core-public.savedobjectsfindoptions.sortorder.md) | estypes.SortOrder | | | [type](./kibana-plugin-core-public.savedobjectsfindoptions.type.md) | string | string[] | | | [typeToNamespacesMap](./kibana-plugin-core-public.savedobjectsfindoptions.typetonamespacesmap.md) | Map<string, string[] | undefined> | This map defines each type to search for, and the namespace(s) to search for the type in; this is only intended to be used by a saved object client wrapper. If this is defined, it supersedes the type and namespaces fields when building the Elasticsearch query. Any types that are not included in this map will be excluded entirely. If a type is included but its value is undefined, the operation will search for that type in the Default namespace. | diff --git a/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.searchafter.md b/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.searchafter.md index 99ca2c34e77be9..7016e1f1b72de5 100644 --- a/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.searchafter.md +++ b/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.searchafter.md @@ -9,5 +9,5 @@ Use the sort values from the previous page to retrieve the next page of results. Signature: ```typescript -searchAfter?: unknown[]; +searchAfter?: estypes.Id[]; ``` diff --git a/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.sortorder.md b/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.sortorder.md index 3834c802fa1845..36f99e51ea8c65 100644 --- a/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.sortorder.md +++ b/docs/development/core/public/kibana-plugin-core-public.savedobjectsfindoptions.sortorder.md @@ -7,5 +7,5 @@ Signature: ```typescript -sortOrder?: string; +sortOrder?: estypes.SortOrder; ``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.md index 6f7c05ea469bc6..a92b1f48d08ebc 100644 --- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.md +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.md @@ -27,10 +27,10 @@ export interface SavedObjectsFindOptions | [preference](./kibana-plugin-core-server.savedobjectsfindoptions.preference.md) | string | An optional ES preference value to be used for the query \* | | [rootSearchFields](./kibana-plugin-core-server.savedobjectsfindoptions.rootsearchfields.md) | string[] | The fields to perform the parsed query against. Unlike the searchFields argument, these are expected to be root fields and will not be modified. If used in conjunction with searchFields, both are concatenated together. | | [search](./kibana-plugin-core-server.savedobjectsfindoptions.search.md) | string | Search documents using the Elasticsearch Simple Query String syntax. See Elasticsearch Simple Query String query argument for more information | -| [searchAfter](./kibana-plugin-core-server.savedobjectsfindoptions.searchafter.md) | unknown[] | Use the sort values from the previous page to retrieve the next page of results. | +| [searchAfter](./kibana-plugin-core-server.savedobjectsfindoptions.searchafter.md) | estypes.Id[] | Use the sort values from the previous page to retrieve the next page of results. | | [searchFields](./kibana-plugin-core-server.savedobjectsfindoptions.searchfields.md) | string[] | The fields to perform the parsed query against. See Elasticsearch Simple Query String fields argument for more information | | [sortField](./kibana-plugin-core-server.savedobjectsfindoptions.sortfield.md) | string | | -| [sortOrder](./kibana-plugin-core-server.savedobjectsfindoptions.sortorder.md) | string | | +| [sortOrder](./kibana-plugin-core-server.savedobjectsfindoptions.sortorder.md) | estypes.SortOrder | | | [type](./kibana-plugin-core-server.savedobjectsfindoptions.type.md) | string | string[] | | | [typeToNamespacesMap](./kibana-plugin-core-server.savedobjectsfindoptions.typetonamespacesmap.md) | Map<string, string[] | undefined> | This map defines each type to search for, and the namespace(s) to search for the type in; this is only intended to be used by a saved object client wrapper. If this is defined, it supersedes the type and namespaces fields when building the Elasticsearch query. Any types that are not included in this map will be excluded entirely. If a type is included but its value is undefined, the operation will search for that type in the Default namespace. | diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.searchafter.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.searchafter.md index 6364370948976e..9afd602259a78e 100644 --- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.searchafter.md +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.searchafter.md @@ -9,5 +9,5 @@ Use the sort values from the previous page to retrieve the next page of results. Signature: ```typescript -searchAfter?: unknown[]; +searchAfter?: estypes.Id[]; ``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.sortorder.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.sortorder.md index d247b9e38e4489..e1c657e3a5171f 100644 --- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.sortorder.md +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindoptions.sortorder.md @@ -7,5 +7,5 @@ Signature: ```typescript -sortOrder?: string; +sortOrder?: estypes.SortOrder; ``` diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.md index 0f8e9c59236bbc..a729ce32e1c802 100644 --- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.md +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.md @@ -16,5 +16,5 @@ export interface SavedObjectsFindResult extends SavedObject | Property | Type | Description | | --- | --- | --- | | [score](./kibana-plugin-core-server.savedobjectsfindresult.score.md) | number | The Elasticsearch _score of this result. | -| [sort](./kibana-plugin-core-server.savedobjectsfindresult.sort.md) | unknown[] | The Elasticsearch sort value of this result. | +| [sort](./kibana-plugin-core-server.savedobjectsfindresult.sort.md) | string[] | The Elasticsearch sort value of this result. | diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.sort.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.sort.md index 17f52687243321..e73d6b4926d893 100644 --- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.sort.md +++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsfindresult.sort.md @@ -9,7 +9,7 @@ The Elasticsearch `sort` value of this result. Signature: ```typescript -sort?: unknown[]; +sort?: string[]; ``` ## Remarks diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.iessearchresponse.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.iessearchresponse.md index c8a372edbdb85a..073b1d462986c6 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.iessearchresponse.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.iessearchresponse.md @@ -7,5 +7,5 @@ Signature: ```typescript -export declare type IEsSearchResponse = IKibanaSearchResponse>; +export declare type IEsSearchResponse = IKibanaSearchResponse>; ``` diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.fetch.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.fetch.md index e96fe8b8e08dc6..623d6366d4d131 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.fetch.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.searchsource.fetch.md @@ -14,7 +14,7 @@ Fetch this source and reject the returned Promise on error Signature: ```typescript -fetch(options?: ISearchOptions): Promise>; +fetch(options?: ISearchOptions): Promise>; ``` ## Parameters @@ -25,5 +25,5 @@ fetch(options?: ISearchOptions): PromiseReturns: -`Promise>` +`Promise>` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.iessearchresponse.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.iessearchresponse.md index d333af1b278c2d..be208c0a51c81e 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.iessearchresponse.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.iessearchresponse.md @@ -7,5 +7,5 @@ Signature: ```typescript -export declare type IEsSearchResponse = IKibanaSearchResponse>; +export declare type IEsSearchResponse = IKibanaSearchResponse>; ``` diff --git a/examples/search_examples/public/search/app.tsx b/examples/search_examples/public/search/app.tsx index 8822be035a3d1c..c87bf21e0e71cb 100644 --- a/examples/search_examples/public/search/app.tsx +++ b/examples/search_examples/public/search/app.tsx @@ -145,7 +145,8 @@ export const SearchExamplesApp = ({ setResponse(res.rawResponse); setTimeTook(res.rawResponse.took); const avgResult: number | undefined = res.rawResponse.aggregations - ? res.rawResponse.aggregations[1].value + ? // @ts-expect-error @elastic/elasticsearch no way to declare a type for aggregation in the search response + res.rawResponse.aggregations[1].value : undefined; const message = ( diff --git a/examples/search_examples/public/search_sessions/app.tsx b/examples/search_examples/public/search_sessions/app.tsx index bf57964dc1f86e..a768600db24eea 100644 --- a/examples/search_examples/public/search_sessions/app.tsx +++ b/examples/search_examples/public/search_sessions/app.tsx @@ -702,13 +702,15 @@ function doSearch( const startTs = performance.now(); // Submit the search request using the `data.search` service. + // @ts-expect-error request.params is incompatible. Filter is not assignable to QueryContainer return data.search .search(req, { sessionId }) .pipe( tap((res) => { if (isCompleteResponse(res)) { const avgResult: number | undefined = res.rawResponse.aggregations - ? res.rawResponse.aggregations[1]?.value ?? res.rawResponse.aggregations[2]?.value + ? // @ts-expect-error @elastic/elasticsearch no way to declare a type for aggregation in the search response + res.rawResponse.aggregations[1]?.value ?? res.rawResponse.aggregations[2]?.value : undefined; const message = ( diff --git a/package.json b/package.json index 7d9a3cfaad13f5..a7dc8db8a44202 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,7 @@ }, "dependencies": { "@elastic/datemath": "link:packages/elastic-datemath", - "@elastic/elasticsearch": "npm:@elastic/elasticsearch-canary@^7.12.0-canary.4", + "@elastic/elasticsearch": "npm:@elastic/elasticsearch-canary@7.13.0-canary.1", "@elastic/ems-client": "7.12.0", "@elastic/eui": "31.7.0", "@elastic/filesaver": "1.1.2", diff --git a/packages/kbn-es-archiver/src/actions/empty_kibana_index.ts b/packages/kbn-es-archiver/src/actions/empty_kibana_index.ts index 821e3a872820ea..3c09b02555c7d6 100644 --- a/packages/kbn-es-archiver/src/actions/empty_kibana_index.ts +++ b/packages/kbn-es-archiver/src/actions/empty_kibana_index.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { Client } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { ToolingLog } from '@kbn/dev-utils'; import { KbnClient } from '@kbn/test'; @@ -17,7 +17,7 @@ export async function emptyKibanaIndexAction({ log, kbnClient, }: { - client: Client; + client: KibanaClient; log: ToolingLog; kbnClient: KbnClient; }) { @@ -25,7 +25,7 @@ export async function emptyKibanaIndexAction({ const kibanaPluginIds = await kbnClient.plugins.getEnabledIds(); await cleanKibanaIndices({ client, stats, log, kibanaPluginIds }); - await migrateKibanaIndex({ client, kbnClient }); + await migrateKibanaIndex({ kbnClient }); stats.createdIndex('.kibana'); return stats.toJSON(); } diff --git a/packages/kbn-es-archiver/src/actions/load.ts b/packages/kbn-es-archiver/src/actions/load.ts index bfa12938da4769..2390fafcf4d9bb 100644 --- a/packages/kbn-es-archiver/src/actions/load.ts +++ b/packages/kbn-es-archiver/src/actions/load.ts @@ -11,7 +11,7 @@ import { createReadStream } from 'fs'; import { Readable } from 'stream'; import { ToolingLog } from '@kbn/dev-utils'; import { KbnClient } from '@kbn/test'; -import { Client } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { createPromiseFromStreams, concatStreamProviders } from '@kbn/utils'; import { ES_CLIENT_HEADERS } from '../client_headers'; @@ -48,7 +48,7 @@ export async function loadAction({ name: string; skipExisting: boolean; useCreate: boolean; - client: Client; + client: KibanaClient; dataDir: string; log: ToolingLog; kbnClient: KbnClient; @@ -103,7 +103,7 @@ export async function loadAction({ // If we affected the Kibana index, we need to ensure it's migrated... if (Object.keys(result).some((k) => k.startsWith('.kibana'))) { - await migrateKibanaIndex({ client, kbnClient }); + await migrateKibanaIndex({ kbnClient }); log.debug('[%s] Migrated Kibana index after loading Kibana data', name); if (kibanaPluginIds.includes('spaces')) { diff --git a/packages/kbn-es-archiver/src/actions/save.ts b/packages/kbn-es-archiver/src/actions/save.ts index 3790e0f013ee03..c90f241a1c6399 100644 --- a/packages/kbn-es-archiver/src/actions/save.ts +++ b/packages/kbn-es-archiver/src/actions/save.ts @@ -9,7 +9,7 @@ import { resolve } from 'path'; import { createWriteStream, mkdirSync } from 'fs'; import { Readable, Writable } from 'stream'; -import { Client } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { ToolingLog } from '@kbn/dev-utils'; import { createListStream, createPromiseFromStreams } from '@kbn/utils'; @@ -32,7 +32,7 @@ export async function saveAction({ }: { name: string; indices: string | string[]; - client: Client; + client: KibanaClient; dataDir: string; log: ToolingLog; raw: boolean; diff --git a/packages/kbn-es-archiver/src/actions/unload.ts b/packages/kbn-es-archiver/src/actions/unload.ts index b5f259a1496bb8..f4e37871a53371 100644 --- a/packages/kbn-es-archiver/src/actions/unload.ts +++ b/packages/kbn-es-archiver/src/actions/unload.ts @@ -9,7 +9,7 @@ import { resolve } from 'path'; import { createReadStream } from 'fs'; import { Readable, Writable } from 'stream'; -import { Client } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { ToolingLog } from '@kbn/dev-utils'; import { KbnClient } from '@kbn/test'; import { createPromiseFromStreams } from '@kbn/utils'; @@ -32,7 +32,7 @@ export async function unloadAction({ kbnClient, }: { name: string; - client: Client; + client: KibanaClient; dataDir: string; log: ToolingLog; kbnClient: KbnClient; diff --git a/packages/kbn-es-archiver/src/client_headers.ts b/packages/kbn-es-archiver/src/client_headers.ts index da240c3ad8318b..5733eb9b97543a 100644 --- a/packages/kbn-es-archiver/src/client_headers.ts +++ b/packages/kbn-es-archiver/src/client_headers.ts @@ -8,4 +8,4 @@ export const ES_CLIENT_HEADERS = { 'x-elastic-product-origin': 'kibana', -}; +} as const; diff --git a/packages/kbn-es-archiver/src/es_archiver.ts b/packages/kbn-es-archiver/src/es_archiver.ts index 68eacb4f3caf22..93ce97efd4c84a 100644 --- a/packages/kbn-es-archiver/src/es_archiver.ts +++ b/packages/kbn-es-archiver/src/es_archiver.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { Client } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { ToolingLog } from '@kbn/dev-utils'; import { KbnClient } from '@kbn/test'; @@ -20,14 +20,14 @@ import { } from './actions'; interface Options { - client: Client; + client: KibanaClient; dataDir: string; log: ToolingLog; kbnClient: KbnClient; } export class EsArchiver { - private readonly client: Client; + private readonly client: KibanaClient; private readonly dataDir: string; private readonly log: ToolingLog; private readonly kbnClient: KbnClient; diff --git a/packages/kbn-es-archiver/src/lib/docs/generate_doc_records_stream.ts b/packages/kbn-es-archiver/src/lib/docs/generate_doc_records_stream.ts index cacd224e714215..88e167b3705cb9 100644 --- a/packages/kbn-es-archiver/src/lib/docs/generate_doc_records_stream.ts +++ b/packages/kbn-es-archiver/src/lib/docs/generate_doc_records_stream.ts @@ -7,7 +7,7 @@ */ import { Transform } from 'stream'; -import { Client } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { Stats } from '../stats'; import { Progress } from '../progress'; import { ES_CLIENT_HEADERS } from '../../client_headers'; @@ -21,7 +21,7 @@ export function createGenerateDocRecordsStream({ progress, query, }: { - client: Client; + client: KibanaClient; stats: Stats; progress: Progress; query?: Record; diff --git a/packages/kbn-es-archiver/src/lib/docs/index_doc_records_stream.ts b/packages/kbn-es-archiver/src/lib/docs/index_doc_records_stream.ts index fc6446688de95b..a5a011ad2a9fe6 100644 --- a/packages/kbn-es-archiver/src/lib/docs/index_doc_records_stream.ts +++ b/packages/kbn-es-archiver/src/lib/docs/index_doc_records_stream.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { Client } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import AggregateError from 'aggregate-error'; import { Writable } from 'stream'; import { Stats } from '../stats'; @@ -14,7 +14,7 @@ import { Progress } from '../progress'; import { ES_CLIENT_HEADERS } from '../../client_headers'; export function createIndexDocRecordsStream( - client: Client, + client: KibanaClient, stats: Stats, progress: Progress, useCreate: boolean = false diff --git a/packages/kbn-es-archiver/src/lib/indices/__mocks__/stubs.ts b/packages/kbn-es-archiver/src/lib/indices/__mocks__/stubs.ts index 59101f54900162..7dde4075dc3f26 100644 --- a/packages/kbn-es-archiver/src/lib/indices/__mocks__/stubs.ts +++ b/packages/kbn-es-archiver/src/lib/indices/__mocks__/stubs.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { Client } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import sinon from 'sinon'; import { ToolingLog } from '@kbn/dev-utils'; import { Stats } from '../../stats'; @@ -67,7 +67,7 @@ const createEsClientError = (errorType: string) => { const indexAlias = (aliases: Record, index: string) => Object.keys(aliases).find((k) => aliases[k] === index); -type StubClient = Client; +type StubClient = KibanaClient; export const createStubClient = ( existingIndices: string[] = [], diff --git a/packages/kbn-es-archiver/src/lib/indices/create_index_stream.test.ts b/packages/kbn-es-archiver/src/lib/indices/create_index_stream.test.ts index 1b5792dde04415..94bf15ec9c3e9a 100644 --- a/packages/kbn-es-archiver/src/lib/indices/create_index_stream.test.ts +++ b/packages/kbn-es-archiver/src/lib/indices/create_index_stream.test.ts @@ -125,7 +125,6 @@ describe('esArchiver: createCreateIndexStream()', () => { ]); sinon.assert.calledWith(client.indices.create as sinon.SinonSpy, { - method: 'PUT', index: 'index', include_type_name: false, body: { diff --git a/packages/kbn-es-archiver/src/lib/indices/create_index_stream.ts b/packages/kbn-es-archiver/src/lib/indices/create_index_stream.ts index 74d135df54b8ca..b8d9237bc32076 100644 --- a/packages/kbn-es-archiver/src/lib/indices/create_index_stream.ts +++ b/packages/kbn-es-archiver/src/lib/indices/create_index_stream.ts @@ -9,7 +9,8 @@ import { Transform, Readable } from 'stream'; import { inspect } from 'util'; -import { Client } from '@elastic/elasticsearch'; +import { estypes } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { ToolingLog } from '@kbn/dev-utils'; import { Stats } from '../stats'; @@ -18,12 +19,9 @@ import { deleteIndex } from './delete_index'; import { ES_CLIENT_HEADERS } from '../../client_headers'; interface DocRecord { - value: { + value: estypes.IndexState & { index: string; type: string; - settings: Record; - mappings: Record; - aliases: Record; }; } @@ -33,7 +31,7 @@ export function createCreateIndexStream({ skipExisting = false, log, }: { - client: Client; + client: KibanaClient; stats: Stats; skipExisting?: boolean; log: ToolingLog; @@ -69,7 +67,6 @@ export function createCreateIndexStream({ await client.indices.create( { - method: 'PUT', index, include_type_name: isPre7Mapping, body: { diff --git a/packages/kbn-es-archiver/src/lib/indices/delete_index.ts b/packages/kbn-es-archiver/src/lib/indices/delete_index.ts index b5641eec4b9da0..2a42d52e2ca80b 100644 --- a/packages/kbn-es-archiver/src/lib/indices/delete_index.ts +++ b/packages/kbn-es-archiver/src/lib/indices/delete_index.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { Client } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { ToolingLog } from '@kbn/dev-utils'; import { Stats } from '../stats'; import { ES_CLIENT_HEADERS } from '../../client_headers'; @@ -15,7 +15,7 @@ import { ES_CLIENT_HEADERS } from '../../client_headers'; const PENDING_SNAPSHOT_STATUSES = ['INIT', 'STARTED', 'WAITING']; export async function deleteIndex(options: { - client: Client; + client: KibanaClient; stats: Stats; index: string | string[]; log: ToolingLog; @@ -84,7 +84,7 @@ export function isDeleteWhileSnapshotInProgressError(error: any) { * snapshotting this index to complete. */ export async function waitForSnapshotCompletion( - client: Client, + client: KibanaClient, index: string | string[], log: ToolingLog ) { diff --git a/packages/kbn-es-archiver/src/lib/indices/delete_index_stream.ts b/packages/kbn-es-archiver/src/lib/indices/delete_index_stream.ts index db065274a7b3b1..e1552b5ed1e3b2 100644 --- a/packages/kbn-es-archiver/src/lib/indices/delete_index_stream.ts +++ b/packages/kbn-es-archiver/src/lib/indices/delete_index_stream.ts @@ -7,7 +7,7 @@ */ import { Transform } from 'stream'; -import { Client } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { ToolingLog } from '@kbn/dev-utils'; import { Stats } from '../stats'; @@ -15,7 +15,7 @@ import { deleteIndex } from './delete_index'; import { cleanKibanaIndices } from './kibana_index'; export function createDeleteIndexStream( - client: Client, + client: KibanaClient, stats: Stats, log: ToolingLog, kibanaPluginIds: string[] diff --git a/packages/kbn-es-archiver/src/lib/indices/generate_index_records_stream.ts b/packages/kbn-es-archiver/src/lib/indices/generate_index_records_stream.ts index 4e0319c52264f2..6619f1b3a601e2 100644 --- a/packages/kbn-es-archiver/src/lib/indices/generate_index_records_stream.ts +++ b/packages/kbn-es-archiver/src/lib/indices/generate_index_records_stream.ts @@ -7,11 +7,11 @@ */ import { Transform } from 'stream'; -import { Client } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { Stats } from '../stats'; import { ES_CLIENT_HEADERS } from '../../client_headers'; -export function createGenerateIndexRecordsStream(client: Client, stats: Stats) { +export function createGenerateIndexRecordsStream(client: KibanaClient, stats: Stats) { return new Transform({ writableObjectMode: true, readableObjectMode: true, diff --git a/packages/kbn-es-archiver/src/lib/indices/kibana_index.ts b/packages/kbn-es-archiver/src/lib/indices/kibana_index.ts index 57cf05d8f0f73c..ff1c4332699e39 100644 --- a/packages/kbn-es-archiver/src/lib/indices/kibana_index.ts +++ b/packages/kbn-es-archiver/src/lib/indices/kibana_index.ts @@ -8,7 +8,8 @@ import { inspect } from 'util'; -import { Client } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; +import type { estypes } from '@elastic/elasticsearch'; import { ToolingLog } from '@kbn/dev-utils'; import { KbnClient } from '@kbn/test'; import { Stats } from '../stats'; @@ -23,7 +24,7 @@ export async function deleteKibanaIndices({ stats, log, }: { - client: Client; + client: KibanaClient; stats: Stats; log: ToolingLog; }) { @@ -58,10 +59,8 @@ export async function deleteKibanaIndices({ * as is required by migrations. */ export async function migrateKibanaIndex({ - client, kbnClient, }: { - client: Client; kbnClient: KbnClient; }) { await kbnClient.savedObjects.migrate(); @@ -73,22 +72,27 @@ export async function migrateKibanaIndex({ * with .kibana, then filters out any that aren't actually Kibana's core * index (e.g. we don't want to remove .kibana_task_manager or the like). */ -async function fetchKibanaIndices(client: Client) { - const resp = await client.cat.indices( +function isKibanaIndex(index?: string): index is string { + return Boolean( + index && + (/^\.kibana(:?_\d*)?$/.test(index) || + /^\.kibana(_task_manager)?_(pre)?\d+\.\d+\.\d+/.test(index)) + ); +} + +async function fetchKibanaIndices(client: KibanaClient) { + const resp = await client.cat.indices( { index: '.kibana*', format: 'json' }, { headers: ES_CLIENT_HEADERS, } ); - const isKibanaIndex = (index: string) => - /^\.kibana(:?_\d*)?$/.test(index) || - /^\.kibana(_task_manager)?_(pre)?\d+\.\d+\.\d+/.test(index); if (!Array.isArray(resp.body)) { throw new Error(`expected response to be an array ${inspect(resp.body)}`); } - return resp.body.map((x: { index: string }) => x.index).filter(isKibanaIndex); + return resp.body.map((x: { index?: string }) => x.index).filter(isKibanaIndex); } const delay = (delayInMs: number) => new Promise((resolve) => setTimeout(resolve, delayInMs)); @@ -99,7 +103,7 @@ export async function cleanKibanaIndices({ log, kibanaPluginIds, }: { - client: Client; + client: KibanaClient; stats: Stats; log: ToolingLog; kibanaPluginIds: string[]; @@ -126,7 +130,7 @@ export async function cleanKibanaIndices({ }, }, }, - }, + } as estypes.QueryContainer, }, }, { @@ -156,7 +160,13 @@ export async function cleanKibanaIndices({ stats.deletedIndex('.kibana'); } -export async function createDefaultSpace({ index, client }: { index: string; client: Client }) { +export async function createDefaultSpace({ + index, + client, +}: { + index: string; + client: KibanaClient; +}) { await client.create( { index, diff --git a/src/core/public/public.api.md b/src/core/public/public.api.md index 396bf16cbdc6f2..5c034e68a3736a 100644 --- a/src/core/public/public.api.md +++ b/src/core/public/public.api.md @@ -11,6 +11,7 @@ import { ConfigDeprecationProvider } from '@kbn/config'; import { ConfigPath } from '@kbn/config'; import { DetailedPeerCertificate } from 'tls'; import { EnvironmentMode } from '@kbn/config'; +import { estypes } from '@elastic/elasticsearch'; import { EuiBreadcrumb } from '@elastic/eui'; import { EuiButtonEmptyProps } from '@elastic/eui'; import { EuiConfirmModalProps } from '@elastic/eui'; @@ -1225,12 +1226,12 @@ export interface SavedObjectsFindOptions { preference?: string; rootSearchFields?: string[]; search?: string; - searchAfter?: unknown[]; + searchAfter?: estypes.Id[]; searchFields?: string[]; // (undocumented) sortField?: string; // (undocumented) - sortOrder?: string; + sortOrder?: estypes.SortOrder; // (undocumented) type: string | string[]; typeToNamespacesMap?: Map; diff --git a/src/core/server/core_usage_data/core_usage_data_service.test.ts b/src/core/server/core_usage_data/core_usage_data_service.test.ts index 673190d22e4218..d1501618ec3432 100644 --- a/src/core/server/core_usage_data/core_usage_data_service.test.ts +++ b/src/core/server/core_usage_data/core_usage_data_service.test.ts @@ -120,10 +120,10 @@ describe('CoreUsageDataService', () => { body: [ { name: '.kibana_task_manager_1', - 'docs.count': 10, - 'docs.deleted': 10, - 'store.size': 1000, - 'pri.store.size': 2000, + 'docs.count': '10', + 'docs.deleted': '10', + 'store.size': '1000', + 'pri.store.size': '2000', }, ], } as any); @@ -131,10 +131,10 @@ describe('CoreUsageDataService', () => { body: [ { name: '.kibana_1', - 'docs.count': 20, - 'docs.deleted': 20, - 'store.size': 2000, - 'pri.store.size': 4000, + 'docs.count': '20', + 'docs.deleted': '20', + 'store.size': '2000', + 'pri.store.size': '4000', }, ], } as any); diff --git a/src/core/server/core_usage_data/core_usage_data_service.ts b/src/core/server/core_usage_data/core_usage_data_service.ts index b9d8c9fc7e39fb..dff68bf1c524fe 100644 --- a/src/core/server/core_usage_data/core_usage_data_service.ts +++ b/src/core/server/core_usage_data/core_usage_data_service.ts @@ -118,10 +118,14 @@ export class CoreUsageDataService implements CoreService; }; -function createApiResponse(opts: Partial = {}): ApiResponse { +function createApiResponse>( + opts: Partial> = {} +): ApiResponse { return { - body: {}, + body: {} as any, statusCode: 200, headers: {}, warnings: [], diff --git a/src/core/server/elasticsearch/client/retry_call_cluster.test.ts b/src/core/server/elasticsearch/client/retry_call_cluster.test.ts index 7b442469838f66..636841316941bb 100644 --- a/src/core/server/elasticsearch/client/retry_call_cluster.test.ts +++ b/src/core/server/elasticsearch/client/retry_call_cluster.test.ts @@ -11,7 +11,7 @@ import { elasticsearchClientMock } from './mocks'; import { loggingSystemMock } from '../../logging/logging_system.mock'; import { retryCallCluster, migrationRetryCallCluster } from './retry_call_cluster'; -const dummyBody = { foo: 'bar' }; +const dummyBody: any = { foo: 'bar' }; const createErrorReturn = (err: any) => elasticsearchClientMock.createErrorTransportRequestPromise(err); @@ -29,7 +29,7 @@ describe('retryCallCluster', () => { client.asyncSearch.get.mockReturnValue(successReturn); - const result = await retryCallCluster(() => client.asyncSearch.get()); + const result = await retryCallCluster(() => client.asyncSearch.get({} as any)); expect(result.body).toEqual(dummyBody); }); @@ -44,7 +44,7 @@ describe('retryCallCluster', () => { ) .mockImplementationOnce(() => successReturn); - const result = await retryCallCluster(() => client.asyncSearch.get()); + const result = await retryCallCluster(() => client.asyncSearch.get({} as any)); expect(result.body).toEqual(dummyBody); }); diff --git a/src/core/server/saved_objects/export/saved_objects_exporter.test.ts b/src/core/server/saved_objects/export/saved_objects_exporter.test.ts index 22dcc8022858c1..468a761781365c 100644 --- a/src/core/server/saved_objects/export/saved_objects_exporter.test.ts +++ b/src/core/server/saved_objects/export/saved_objects_exporter.test.ts @@ -117,6 +117,7 @@ describe('getSortedObjectsForExport()', () => { "keepAlive": "2m", }, "search": undefined, + "searchAfter": undefined, "sortField": "updated_at", "sortOrder": "desc", "type": Array [ @@ -145,7 +146,7 @@ describe('getSortedObjectsForExport()', () => { type = 'index-pattern', }: { attributes?: Record; - sort?: unknown[]; + sort?: string[]; type?: string; } = {} ) { @@ -461,6 +462,7 @@ describe('getSortedObjectsForExport()', () => { "keepAlive": "2m", }, "search": undefined, + "searchAfter": undefined, "sortField": "updated_at", "sortOrder": "desc", "type": Array [ @@ -617,6 +619,7 @@ describe('getSortedObjectsForExport()', () => { "keepAlive": "2m", }, "search": "foo", + "searchAfter": undefined, "sortField": "updated_at", "sortOrder": "desc", "type": Array [ @@ -710,6 +713,7 @@ describe('getSortedObjectsForExport()', () => { "keepAlive": "2m", }, "search": undefined, + "searchAfter": undefined, "sortField": "updated_at", "sortOrder": "desc", "type": Array [ @@ -808,6 +812,7 @@ describe('getSortedObjectsForExport()', () => { "keepAlive": "2m", }, "search": undefined, + "searchAfter": undefined, "sortField": "updated_at", "sortOrder": "desc", "type": Array [ diff --git a/src/core/server/saved_objects/import/lib/check_origin_conflicts.ts b/src/core/server/saved_objects/import/lib/check_origin_conflicts.ts index d35388ff947498..1952a04ab815ca 100644 --- a/src/core/server/saved_objects/import/lib/check_origin_conflicts.ts +++ b/src/core/server/saved_objects/import/lib/check_origin_conflicts.ts @@ -95,7 +95,7 @@ const checkOriginConflict = async ( perPage: 10, fields: ['title'], sortField: 'updated_at', - sortOrder: 'desc', + sortOrder: 'desc' as const, ...(namespace && { namespaces: [namespace] }), }; const findResult = await savedObjectsClient.find<{ title?: string }>(findOptions); diff --git a/src/core/server/saved_objects/mappings/types.ts b/src/core/server/saved_objects/mappings/types.ts index fa7531392d1229..25fb61de935187 100644 --- a/src/core/server/saved_objects/mappings/types.ts +++ b/src/core/server/saved_objects/mappings/types.ts @@ -102,7 +102,7 @@ export type SavedObjectsFieldMapping = /** @internal */ export interface IndexMapping { - dynamic?: string; + dynamic?: boolean | 'strict'; properties: SavedObjectsMappingProperties; _meta?: IndexMappingMeta; } diff --git a/src/core/server/saved_objects/migrations/core/build_active_mappings.test.ts b/src/core/server/saved_objects/migrations/core/build_active_mappings.test.ts index 63634bdb1754ea..5465da2f620adb 100644 --- a/src/core/server/saved_objects/migrations/core/build_active_mappings.test.ts +++ b/src/core/server/saved_objects/migrations/core/build_active_mappings.test.ts @@ -164,6 +164,7 @@ describe('diffMappings', () => { _meta: { migrationMappingPropertyHashes: { foo: 'bar' }, }, + // @ts-expect-error dynamic: 'abcde', properties: {}, }; diff --git a/src/core/server/saved_objects/migrations/core/call_cluster.ts b/src/core/server/saved_objects/migrations/core/call_cluster.ts index bbf39549457d8a..f37bbdd14a8994 100644 --- a/src/core/server/saved_objects/migrations/core/call_cluster.ts +++ b/src/core/server/saved_objects/migrations/core/call_cluster.ts @@ -12,11 +12,12 @@ * funcationality contained here. */ +import type { estypes } from '@elastic/elasticsearch'; import { IndexMapping } from '../../mappings'; export interface CallCluster { (path: 'bulk', opts: { body: object[] }): Promise; - (path: 'count', opts: CountOpts): Promise<{ count: number; _shards: ShardsInfo }>; + (path: 'count', opts: CountOpts): Promise<{ count: number; _shards: estypes.ShardStatistics }>; (path: 'clearScroll', opts: { scrollId: string }): Promise; (path: 'indices.create', opts: IndexCreationOpts): Promise; (path: 'indices.exists', opts: IndexOpts): Promise; @@ -143,7 +144,7 @@ export interface IndexSettingsResult { } export interface RawDoc { - _id: string; + _id: estypes.Id; _source: any; _type?: string; } @@ -153,14 +154,7 @@ export interface SearchResults { hits: RawDoc[]; }; _scroll_id?: string; - _shards: ShardsInfo; -} - -export interface ShardsInfo { - total: number; - successful: number; - skipped: number; - failed: number; + _shards: estypes.ShardStatistics; } export interface ErrorResponse { diff --git a/src/core/server/saved_objects/migrations/core/elastic_index.test.ts b/src/core/server/saved_objects/migrations/core/elastic_index.test.ts index bfa686ac0cc47e..5cb2a88c4733f7 100644 --- a/src/core/server/saved_objects/migrations/core/elastic_index.test.ts +++ b/src/core/server/saved_objects/migrations/core/elastic_index.test.ts @@ -6,6 +6,7 @@ * Side Public License, v 1. */ +import type { estypes } from '@elastic/elasticsearch'; import _ from 'lodash'; import { elasticsearchClientMock } from '../../../elasticsearch/client/mocks'; import * as Index from './elastic_index'; @@ -33,41 +34,6 @@ describe('ElasticIndex', () => { expect(client.indices.get).toHaveBeenCalledWith({ index: '.kibana-test' }, { ignore: [404] }); }); - test('fails if the index doc type is unsupported', async () => { - client.indices.get.mockImplementation((params) => { - const index = params!.index as string; - return elasticsearchClientMock.createSuccessTransportRequestPromise({ - [index]: { - aliases: { foo: index }, - mappings: { spock: { dynamic: 'strict', properties: { a: 'b' } } }, - }, - }); - }); - - await expect(Index.fetchInfo(client, '.baz')).rejects.toThrow( - /cannot be automatically migrated/ - ); - }); - - test('fails if there are multiple root types', async () => { - client.indices.get.mockImplementation((params) => { - const index = params!.index as string; - return elasticsearchClientMock.createSuccessTransportRequestPromise({ - [index]: { - aliases: { foo: index }, - mappings: { - doc: { dynamic: 'strict', properties: { a: 'b' } }, - doctor: { dynamic: 'strict', properties: { a: 'b' } }, - }, - }, - }); - }); - - await expect(Index.fetchInfo(client, '.baz')).rejects.toThrow( - /cannot be automatically migrated/ - ); - }); - test('decorates index info with exists and indexName', async () => { client.indices.get.mockImplementation((params) => { const index = params!.index as string; @@ -75,8 +41,9 @@ describe('ElasticIndex', () => { [index]: { aliases: { foo: index }, mappings: { dynamic: 'strict', properties: { a: 'b' } }, + settings: {}, }, - }); + } as estypes.GetIndexResponse); }); const info = await Index.fetchInfo(client, '.baz'); @@ -85,6 +52,7 @@ describe('ElasticIndex', () => { mappings: { dynamic: 'strict', properties: { a: 'b' } }, exists: true, indexName: '.baz', + settings: {}, }); }); }); @@ -134,7 +102,7 @@ describe('ElasticIndex', () => { test('removes existing alias', async () => { client.indices.getAlias.mockResolvedValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ - '.my-fanci-index': '.muchacha', + '.my-fanci-index': { aliases: { '.muchacha': {} } }, }) ); @@ -157,7 +125,7 @@ describe('ElasticIndex', () => { test('allows custom alias actions', async () => { client.indices.getAlias.mockResolvedValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ - '.my-fanci-index': '.muchacha', + '.my-fanci-index': { aliases: { '.muchacha': {} } }, }) ); @@ -185,14 +153,18 @@ describe('ElasticIndex', () => { test('it creates the destination index, then reindexes to it', async () => { client.indices.getAlias.mockResolvedValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ - '.my-fanci-index': '.muchacha', + '.my-fanci-index': { aliases: { '.muchacha': {} } }, }) ); client.reindex.mockResolvedValue( - elasticsearchClientMock.createSuccessTransportRequestPromise({ task: 'abc' }) + elasticsearchClientMock.createSuccessTransportRequestPromise({ + task: 'abc', + } as estypes.ReindexResponse) ); client.tasks.get.mockResolvedValue( - elasticsearchClientMock.createSuccessTransportRequestPromise({ completed: true }) + elasticsearchClientMock.createSuccessTransportRequestPromise({ + completed: true, + } as estypes.GetTaskResponse) ); const info = { @@ -200,7 +172,7 @@ describe('ElasticIndex', () => { exists: true, indexName: '.ze-index', mappings: { - dynamic: 'strict', + dynamic: 'strict' as const, properties: { foo: { type: 'keyword' } }, }, }; @@ -259,13 +231,16 @@ describe('ElasticIndex', () => { test('throws error if re-index task fails', async () => { client.indices.getAlias.mockResolvedValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ - '.my-fanci-index': '.muchacha', + '.my-fanci-index': { aliases: { '.muchacha': {} } }, }) ); client.reindex.mockResolvedValue( - elasticsearchClientMock.createSuccessTransportRequestPromise({ task: 'abc' }) + elasticsearchClientMock.createSuccessTransportRequestPromise({ + task: 'abc', + } as estypes.ReindexResponse) ); client.tasks.get.mockResolvedValue( + // @ts-expect-error @elastic/elasticsearch GetTaskResponse requires a `task` property even on errors elasticsearchClientMock.createSuccessTransportRequestPromise({ completed: true, error: { @@ -273,7 +248,7 @@ describe('ElasticIndex', () => { reason: 'all shards failed', failed_shards: [], }, - }) + } as estypes.GetTaskResponse) ); const info = { @@ -286,6 +261,7 @@ describe('ElasticIndex', () => { }, }; + // @ts-expect-error await expect(Index.convertToAlias(client, info, '.muchacha', 10)).rejects.toThrow( /Re-index failed \[search_phase_execution_exception\] all shards failed/ ); @@ -319,7 +295,9 @@ describe('ElasticIndex', () => { describe('write', () => { test('writes documents in bulk to the index', async () => { client.bulk.mockResolvedValue( - elasticsearchClientMock.createSuccessTransportRequestPromise({ items: [] }) + elasticsearchClientMock.createSuccessTransportRequestPromise({ + items: [] as any[], + } as estypes.BulkResponse) ); const index = '.myalias'; @@ -356,7 +334,7 @@ describe('ElasticIndex', () => { client.bulk.mockResolvedValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ items: [{ index: { error: { type: 'shazm', reason: 'dern' } } }], - }) + } as estypes.BulkResponse) ); const index = '.myalias'; diff --git a/src/core/server/saved_objects/migrations/core/elastic_index.ts b/src/core/server/saved_objects/migrations/core/elastic_index.ts index e42643565eb4fe..a5f3cb36e736b6 100644 --- a/src/core/server/saved_objects/migrations/core/elastic_index.ts +++ b/src/core/server/saved_objects/migrations/core/elastic_index.ts @@ -12,11 +12,12 @@ */ import _ from 'lodash'; +import { estypes } from '@elastic/elasticsearch'; import { MigrationEsClient } from './migration_es_client'; import { CountResponse, SearchResponse } from '../../../elasticsearch'; import { IndexMapping } from '../../mappings'; import { SavedObjectsMigrationVersion } from '../../types'; -import { AliasAction, RawDoc, ShardsInfo } from './call_cluster'; +import { AliasAction, RawDoc } from './call_cluster'; import { SavedObjectsRawDocSource } from '../../serialization'; const settings = { number_of_shards: 1, auto_expand_replicas: '0-1' }; @@ -46,6 +47,7 @@ export async function fetchInfo(client: MigrationEsClient, index: string): Promi const [indexName, indexInfo] = Object.entries(body)[0]; + // @ts-expect-error @elastic/elasticsearch IndexState.alias and IndexState.mappings should be required return assertIsSupportedIndex({ ...indexInfo, exists: true, indexName }); } @@ -142,7 +144,7 @@ export async function write(client: MigrationEsClient, index: string, docs: RawD return; } - const exception: any = new Error(err.index.error!.reason); + const exception: any = new Error(err.index!.error!.reason); exception.detail = err; throw exception; } @@ -322,7 +324,7 @@ function assertIsSupportedIndex(indexInfo: FullIndexInfo) { * Object indices should only ever have a single shard. This is more to handle * instances where customers manually expand the shards of an index. */ -function assertResponseIncludeAllShards({ _shards }: { _shards: ShardsInfo }) { +function assertResponseIncludeAllShards({ _shards }: { _shards: estypes.ShardStatistics }) { if (!_.has(_shards, 'total') || !_.has(_shards, 'successful')) { return; } @@ -375,11 +377,12 @@ async function reindex( await new Promise((r) => setTimeout(r, pollInterval)); const { body } = await client.tasks.get({ - task_id: task, + task_id: String(task), }); - if (body.error) { - const e = body.error; + // @ts-expect-error @elastic/elasticsearch GetTaskResponse doesn't contain `error` property + const e = body.error; + if (e) { throw new Error(`Re-index failed [${e.type}] ${e.reason} :: ${JSON.stringify(e)}`); } diff --git a/src/core/server/saved_objects/migrations/core/index_migrator.test.ts b/src/core/server/saved_objects/migrations/core/index_migrator.test.ts index 0d1939231ce6c8..dd295efacf6b86 100644 --- a/src/core/server/saved_objects/migrations/core/index_migrator.test.ts +++ b/src/core/server/saved_objects/migrations/core/index_migrator.test.ts @@ -7,6 +7,7 @@ */ import _ from 'lodash'; +import type { estypes } from '@elastic/elasticsearch'; import { elasticsearchClientMock } from '../../../elasticsearch/client/mocks'; import { SavedObjectUnsanitizedDoc, SavedObjectsSerializer } from '../../serialization'; import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry'; @@ -443,23 +444,28 @@ function withIndex( elasticsearchClientMock.createSuccessTransportRequestPromise({ task: 'zeid', _shards: { successful: 1, total: 1 }, - }) + } as estypes.ReindexResponse) ); client.tasks.get.mockReturnValue( - elasticsearchClientMock.createSuccessTransportRequestPromise({ completed: true }) + elasticsearchClientMock.createSuccessTransportRequestPromise({ + completed: true, + } as estypes.GetTaskResponse) ); client.search.mockReturnValue( - elasticsearchClientMock.createSuccessTransportRequestPromise(searchResult(0)) + elasticsearchClientMock.createSuccessTransportRequestPromise(searchResult(0) as any) ); client.bulk.mockReturnValue( - elasticsearchClientMock.createSuccessTransportRequestPromise({ items: [] }) + elasticsearchClientMock.createSuccessTransportRequestPromise({ + items: [] as any[], + } as estypes.BulkResponse) ); client.count.mockReturnValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ count: numOutOfDate, _shards: { successful: 1, total: 1 }, - }) + } as estypes.CountResponse) ); + // @ts-expect-error client.scroll.mockImplementation(() => { if (scrollCallCounter <= docs.length) { const result = searchResult(scrollCallCounter); diff --git a/src/core/server/saved_objects/migrations/core/index_migrator.ts b/src/core/server/saved_objects/migrations/core/index_migrator.ts index 52f155e5d2de21..5bf5ae26f6a0ad 100644 --- a/src/core/server/saved_objects/migrations/core/index_migrator.ts +++ b/src/core/server/saved_objects/migrations/core/index_migrator.ts @@ -134,7 +134,7 @@ async function deleteIndexTemplates({ client, log, obsoleteIndexTemplatePattern return; } - const { body: templates } = await client.cat.templates>({ + const { body: templates } = await client.cat.templates({ format: 'json', name: obsoleteIndexTemplatePattern, }); @@ -147,7 +147,7 @@ async function deleteIndexTemplates({ client, log, obsoleteIndexTemplatePattern log.info(`Removing index templates: ${templateNames}`); - return Promise.all(templateNames.map((name) => client.indices.deleteTemplate({ name }))); + return Promise.all(templateNames.map((name) => client.indices.deleteTemplate({ name: name! }))); } /** @@ -185,7 +185,13 @@ async function migrateSourceToDest(context: Context) { await Index.write( client, dest.indexName, - await migrateRawDocs(serializer, documentMigrator.migrateAndConvert, docs, log) + await migrateRawDocs( + serializer, + documentMigrator.migrateAndConvert, + // @ts-expect-error @elastic/elasticsearch `Hit._id` may be a string | number in ES, but we always expect strings in the SO index. + docs, + log + ) ); } } diff --git a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts index b8accc462df9a0..7ead37699980a5 100644 --- a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts +++ b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts @@ -7,13 +7,13 @@ */ import { take } from 'rxjs/operators'; +import { estypes, errors as esErrors } from '@elastic/elasticsearch'; import { elasticsearchClientMock } from '../../../elasticsearch/client/mocks'; import { KibanaMigratorOptions, KibanaMigrator } from './kibana_migrator'; import { loggingSystemMock } from '../../../logging/logging_system.mock'; import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry'; import { SavedObjectsType } from '../../types'; -import { errors as esErrors } from '@elastic/elasticsearch'; import { DocumentMigrator } from '../core/document_migrator'; jest.mock('../core/document_migrator', () => { return { @@ -105,10 +105,7 @@ describe('KibanaMigrator', () => { const options = mockOptions(); options.client.cat.templates.mockReturnValue( - elasticsearchClientMock.createSuccessTransportRequestPromise( - { templates: [] }, - { statusCode: 404 } - ) + elasticsearchClientMock.createSuccessTransportRequestPromise([], { statusCode: 404 }) ); options.client.indices.get.mockReturnValue( elasticsearchClientMock.createSuccessTransportRequestPromise({}, { statusCode: 404 }) @@ -129,7 +126,8 @@ describe('KibanaMigrator', () => { options.client.cat.templates.mockReturnValue( elasticsearchClientMock.createSuccessTransportRequestPromise( - { templates: [] }, + // @ts-expect-error + { templates: [] } as CatTemplatesResponse, { statusCode: 404 } ) ); @@ -155,7 +153,8 @@ describe('KibanaMigrator', () => { options.client.cat.templates.mockReturnValue( elasticsearchClientMock.createSuccessTransportRequestPromise( - { templates: [] }, + // @ts-expect-error + { templates: [] } as CatTemplatesResponse, { statusCode: 404 } ) ); @@ -193,7 +192,8 @@ describe('KibanaMigrator', () => { options.client.cat.templates.mockReturnValue( elasticsearchClientMock.createSuccessTransportRequestPromise( - { templates: [] }, + // @ts-expect-error + { templates: [] } as CatTemplatesResponse, { statusCode: 404 } ) ); @@ -323,7 +323,7 @@ describe('KibanaMigrator', () => { completed: true, error: { type: 'elatsicsearch_exception', reason: 'task failed with an error' }, failures: [], - task: { description: 'task description' }, + task: { description: 'task description' } as any, }) ); @@ -365,15 +365,17 @@ const mockV2MigrationOptions = () => { elasticsearchClientMock.createSuccessTransportRequestPromise({ acknowledged: true }) ); options.client.reindex.mockReturnValue( - elasticsearchClientMock.createSuccessTransportRequestPromise({ taskId: 'reindex_task_id' }) + elasticsearchClientMock.createSuccessTransportRequestPromise({ + taskId: 'reindex_task_id', + } as estypes.ReindexResponse) ); options.client.tasks.get.mockReturnValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ completed: true, error: undefined, failures: [], - task: { description: 'task description' }, - }) + task: { description: 'task description' } as any, + } as estypes.GetTaskResponse) ); return options; diff --git a/src/core/server/saved_objects/migrationsv2/actions/index.ts b/src/core/server/saved_objects/migrationsv2/actions/index.ts index d025f104c6e3f1..22dfb03815052d 100644 --- a/src/core/server/saved_objects/migrationsv2/actions/index.ts +++ b/src/core/server/saved_objects/migrationsv2/actions/index.ts @@ -13,9 +13,10 @@ import { ElasticsearchClientError } from '@elastic/elasticsearch/lib/errors'; import { pipe } from 'fp-ts/lib/pipeable'; import { errors as EsErrors } from '@elastic/elasticsearch'; import { flow } from 'fp-ts/lib/function'; +import type { estypes } from '@elastic/elasticsearch'; import { ElasticsearchClient } from '../../../elasticsearch'; import { IndexMapping } from '../../mappings'; -import { SavedObjectsRawDoc } from '../../serialization'; +import { SavedObjectsRawDoc, SavedObjectsRawDocSource } from '../../serialization'; import { catchRetryableEsClientErrors, RetryableEsClientError, @@ -56,20 +57,22 @@ export type FetchIndexResponse = Record< export const fetchIndices = ( client: ElasticsearchClient, indicesToFetch: string[] -): TaskEither.TaskEither => () => { - return client.indices - .get( - { - index: indicesToFetch, - ignore_unavailable: true, // Don't return an error for missing indices. Note this *will* include closed indices, the docs are misleading https://github.com/elastic/elasticsearch/issues/63607 - }, - { ignore: [404], maxRetries: 0 } - ) - .then(({ body }) => { - return Either.right(body); - }) - .catch(catchRetryableEsClientErrors); -}; +): TaskEither.TaskEither => + // @ts-expect-error @elastic/elasticsearch IndexState.alias and IndexState.mappings should be required + () => { + return client.indices + .get( + { + index: indicesToFetch, + ignore_unavailable: true, // Don't return an error for missing indices. Note this *will* include closed indices, the docs are misleading https://github.com/elastic/elasticsearch/issues/63607 + }, + { ignore: [404], maxRetries: 0 } + ) + .then(({ body }) => { + return Either.right(body); + }) + .catch(catchRetryableEsClientErrors); + }; /** * Sets a write block in place for the given index. If the response includes @@ -98,7 +101,7 @@ export const setWriteBlock = ( }, { maxRetries: 0 /** handle retry ourselves for now */ } ) - .then((res) => { + .then((res: any) => { return res.body.acknowledged === true ? Either.right('set_write_block_succeeded' as const) : Either.left({ @@ -134,7 +137,11 @@ export const removeWriteBlock = ( // Don't change any existing settings preserve_existing: true, body: { - 'index.blocks.write': false, + index: { + blocks: { + write: false, + }, + }, }, }, { maxRetries: 0 /** handle retry ourselves for now */ } @@ -285,7 +292,7 @@ interface WaitForTaskResponse { error: Option.Option<{ type: string; reason: string; index: string }>; completed: boolean; failures: Option.Option; - description: string; + description?: string; } /** @@ -299,12 +306,7 @@ const waitForTask = ( timeout: string ): TaskEither.TaskEither => () => { return client.tasks - .get<{ - completed: boolean; - response: { failures: any[] }; - task: { description: string }; - error: { type: string; reason: string; index: string }; - }>({ + .get({ task_id: taskId, wait_for_completion: true, timeout, @@ -314,6 +316,7 @@ const waitForTask = ( const failures = body.response?.failures ?? []; return Either.right({ completed: body.completed, + // @ts-expect-error @elastic/elasticsearch GetTaskResponse doesn't declare `error` property error: Option.fromNullable(body.error), failures: failures.length > 0 ? Option.some(failures) : Option.none, description: body.task.description, @@ -359,7 +362,7 @@ export const pickupUpdatedMappings = ( wait_for_completion: false, }) .then(({ body: { task: taskId } }) => { - return Either.right({ taskId }); + return Either.right({ taskId: String(taskId!) }); }) .catch(catchRetryableEsClientErrors); }; @@ -387,7 +390,6 @@ export const reindex = ( .reindex({ // Require targetIndex to be an alias. Prevents a new index from being // created if targetIndex doesn't exist. - // @ts-expect-error This API isn't documented require_alias: requireAlias, body: { // Ignore version conflicts from existing documents @@ -416,7 +418,7 @@ export const reindex = ( wait_for_completion: false, }) .then(({ body: { task: taskId } }) => { - return Either.right({ taskId }); + return Either.right({ taskId: String(taskId) }); }) .catch(catchRetryableEsClientErrors); }; @@ -624,7 +626,7 @@ export const createIndex = ( const aliasesObject = (aliases ?? []).reduce((acc, alias) => { acc[alias] = {}; return acc; - }, {} as Record); + }, {} as Record); return client.indices .create( @@ -727,7 +729,7 @@ export const updateAndPickupMappings = ( 'update_mappings_succeeded' > = () => { return client.indices - .putMapping, IndexMapping>({ + .putMapping({ index, timeout: DEFAULT_TIMEOUT, body: mappings, @@ -774,22 +776,16 @@ export const searchForOutdatedDocuments = ( query: Record ): TaskEither.TaskEither => () => { return client - .search<{ - // when `filter_path` is specified, ES doesn't return empty arrays, so if - // there are no search results res.body.hits will be undefined. - hits?: { - hits?: SavedObjectsRawDoc[]; - }; - }>({ + .search({ index, - // Optimize search performance by sorting by the "natural" index order - sort: ['_doc'], // Return the _seq_no and _primary_term so we can use optimistic // concurrency control for updates seq_no_primary_term: true, size: BATCH_SIZE, body: { query, + // Optimize search performance by sorting by the "natural" index order + sort: ['_doc'], }, // Return an error when targeting missing or closed indices allow_no_indices: false, @@ -811,7 +807,9 @@ export const searchForOutdatedDocuments = ( 'hits.hits._primary_term', ], }) - .then((res) => Either.right({ outdatedDocuments: res.body.hits?.hits ?? [] })) + .then((res) => + Either.right({ outdatedDocuments: (res.body.hits?.hits as SavedObjectsRawDoc[]) ?? [] }) + ) .catch(catchRetryableEsClientErrors); }; @@ -825,20 +823,7 @@ export const bulkOverwriteTransformedDocuments = ( transformedDocs: SavedObjectsRawDoc[] ): TaskEither.TaskEither => () => { return client - .bulk<{ - took: number; - errors: boolean; - items: [ - { - index: { - _id: string; - status: number; - // the filter_path ensures that only items with errors are returned - error: { type: string; reason: string }; - }; - } - ]; - }>({ + .bulk({ // Because we only add aliases in the MARK_VERSION_INDEX_READY step we // can't bulkIndex to an alias with require_alias=true. This means if // users tamper during this operation (delete indices or restore a @@ -880,7 +865,7 @@ export const bulkOverwriteTransformedDocuments = ( // Filter out version_conflict_engine_exception since these just mean // that another instance already updated these documents const errors = (res.body.items ?? []).filter( - (item) => item.index.error.type !== 'version_conflict_engine_exception' + (item) => item.index?.error?.type !== 'version_conflict_engine_exception' ); if (errors.length === 0) { return Either.right('bulk_index_succeeded' as const); diff --git a/src/core/server/saved_objects/migrationsv2/integration_tests/actions.test.ts b/src/core/server/saved_objects/migrationsv2/integration_tests/actions.test.ts index 46cfd935f429b3..2c052a87d028b5 100644 --- a/src/core/server/saved_objects/migrationsv2/integration_tests/actions.test.ts +++ b/src/core/server/saved_objects/migrationsv2/integration_tests/actions.test.ts @@ -258,7 +258,7 @@ describe('migration actions', () => { index: 'clone_red_then_yellow_index', body: { // Enable all shard allocation so that the index status goes yellow - 'index.routing.allocation.enable': 'all', + index: { routing: { allocation: { enable: 'all' } } }, }, }); indexYellow = true; @@ -500,7 +500,7 @@ describe('migration actions', () => { // Create an index with incompatible mappings await createIndex(client, 'reindex_target_6', { - dynamic: 'false', + dynamic: false, properties: { title: { type: 'integer' } }, // integer is incompatible with string title })(); @@ -926,7 +926,7 @@ describe('migration actions', () => { index: 'red_then_yellow_index', body: { // Disable all shard allocation so that the index status is red - 'index.routing.allocation.enable': 'all', + index: { routing: { allocation: { enable: 'all' } } }, }, }); indexYellow = true; diff --git a/src/core/server/saved_objects/migrationsv2/integration_tests/migration.test.ts b/src/core/server/saved_objects/migrationsv2/integration_tests/migration.test.ts index 638e0e7afbc239..197582b732b3b9 100644 --- a/src/core/server/saved_objects/migrationsv2/integration_tests/migration.test.ts +++ b/src/core/server/saved_objects/migrationsv2/integration_tests/migration.test.ts @@ -162,7 +162,9 @@ describe('migration v2', () => { const expectedVersions = getExpectedVersionPerType(); const res = await esClient.search({ index: migratedIndex, - sort: ['_doc'], + body: { + sort: ['_doc'], + }, size: 10000, }); const allDocuments = res.body.hits.hits as SavedObjectsRawDoc[]; diff --git a/src/core/server/saved_objects/migrationsv2/model.ts b/src/core/server/saved_objects/migrationsv2/model.ts index 6f915df9dd9588..2e92f34429ea9f 100644 --- a/src/core/server/saved_objects/migrationsv2/model.ts +++ b/src/core/server/saved_objects/migrationsv2/model.ts @@ -727,7 +727,6 @@ export const createInitialState = ({ }; const reindexTargetMappings: IndexMapping = { - // @ts-expect-error we don't allow plugins to set `dynamic` dynamic: false, properties: { type: { type: 'keyword' }, diff --git a/src/core/server/saved_objects/service/lib/included_fields.ts b/src/core/server/saved_objects/service/lib/included_fields.ts index 16e27bcc12b8f0..cef83f103ec539 100644 --- a/src/core/server/saved_objects/service/lib/included_fields.ts +++ b/src/core/server/saved_objects/service/lib/included_fields.ts @@ -12,7 +12,10 @@ function toArray(value: string | string[]): string[] { /** * Provides an array of paths for ES source filtering */ -export function includedFields(type: string | string[] = '*', fields?: string[] | string) { +export function includedFields( + type: string | string[] = '*', + fields?: string[] | string +): string[] | undefined { if (!fields || fields.length === 0) { return; } diff --git a/src/core/server/saved_objects/service/lib/point_in_time_finder.ts b/src/core/server/saved_objects/service/lib/point_in_time_finder.ts index b8f459151e7b35..9a8dcceafebb28 100644 --- a/src/core/server/saved_objects/service/lib/point_in_time_finder.ts +++ b/src/core/server/saved_objects/service/lib/point_in_time_finder.ts @@ -5,7 +5,7 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ - +import type { estypes } from '@elastic/elasticsearch'; import type { Logger } from '../../../logging'; import type { SavedObjectsFindOptions, SavedObjectsClientContract } from '../../types'; import type { SavedObjectsFindResponse } from '../'; @@ -96,12 +96,12 @@ export class PointInTimeFinder implements ISavedObjectsPointInTimeFinder { await this.open(); let lastResultsCount: number; - let lastHitSortValue: unknown[] | undefined; + let lastHitSortValue: estypes.Id[] | undefined; do { const results = await this.findNext({ findOptions: this.#findOptions, id: this.#pitId, - ...(lastHitSortValue ? { searchAfter: lastHitSortValue } : {}), + searchAfter: lastHitSortValue, }); this.#pitId = results.pit_id; lastResultsCount = results.saved_objects.length; @@ -159,7 +159,7 @@ export class PointInTimeFinder implements ISavedObjectsPointInTimeFinder { }: { findOptions: SavedObjectsFindOptions; id?: string; - searchAfter?: unknown[]; + searchAfter?: estypes.Id[]; }) { try { return await this.#client.find({ @@ -168,8 +168,8 @@ export class PointInTimeFinder implements ISavedObjectsPointInTimeFinder { sortOrder: 'desc', // Bump keep_alive by 2m on every new request to allow for the ES client // to make multiple retries in the event of a network failure. - ...(id ? { pit: { id, keepAlive: '2m' } } : {}), - ...(searchAfter ? { searchAfter } : {}), + pit: id ? { id, keepAlive: '2m' } : undefined, + searchAfter, ...findOptions, }); } catch (e) { @@ -181,7 +181,7 @@ export class PointInTimeFinder implements ISavedObjectsPointInTimeFinder { } } - private getLastHitSortValue(res: SavedObjectsFindResponse): unknown[] | undefined { + private getLastHitSortValue(res: SavedObjectsFindResponse): estypes.Id[] | undefined { if (res.saved_objects.length < 1) { return undefined; } diff --git a/src/core/server/saved_objects/service/lib/repository.test.js b/src/core/server/saved_objects/service/lib/repository.test.js index bff23895fe4592..37572c83e4c885 100644 --- a/src/core/server/saved_objects/service/lib/repository.test.js +++ b/src/core/server/saved_objects/service/lib/repository.test.js @@ -2782,18 +2782,20 @@ describe('SavedObjectsRepository', () => { await findSuccess({ type, fields: ['title'] }); expect(client.search).toHaveBeenCalledWith( expect.objectContaining({ - _source: [ - `${type}.title`, - 'namespace', - 'namespaces', - 'type', - 'references', - 'migrationVersion', - 'coreMigrationVersion', - 'updated_at', - 'originId', - 'title', - ], + body: expect.objectContaining({ + _source: [ + `${type}.title`, + 'namespace', + 'namespaces', + 'type', + 'references', + 'migrationVersion', + 'coreMigrationVersion', + 'updated_at', + 'originId', + 'title', + ], + }), }), expect.anything() ); @@ -3837,6 +3839,7 @@ describe('SavedObjectsRepository', () => { id: '6.0.0-alpha1', ...mockTimestampFields, version: mockVersion, + references: [], attributes: { buildNum: 8468, apiCallsCount: 100, diff --git a/src/core/server/saved_objects/service/lib/repository.ts b/src/core/server/saved_objects/service/lib/repository.ts index a302cfe5a1e6f0..aa1e62c1652cad 100644 --- a/src/core/server/saved_objects/service/lib/repository.ts +++ b/src/core/server/saved_objects/service/lib/repository.ts @@ -7,13 +7,9 @@ */ import { omit, isObject } from 'lodash'; -import { - ElasticsearchClient, - DeleteDocumentResponse, - GetResponse, - SearchResponse, -} from '../../../elasticsearch/'; -import { Logger } from '../../../logging'; +import type { estypes } from '@elastic/elasticsearch'; +import type { ElasticsearchClient } from '../../../elasticsearch/'; +import type { Logger } from '../../../logging'; import { getRootPropertiesObjects, IndexMapping } from '../../mappings'; import { ISavedObjectsPointInTimeFinder, @@ -397,7 +393,7 @@ export class SavedObjectsRepository { _source: ['type', 'namespaces'], })); const bulkGetResponse = bulkGetDocs.length - ? await this.client.mget( + ? await this.client.mget( { body: { docs: bulkGetDocs, @@ -425,8 +421,9 @@ export class SavedObjectsRepository { if (esRequestIndex !== undefined) { const indexFound = bulkGetResponse?.statusCode !== 404; const actualResult = indexFound ? bulkGetResponse?.body.docs[esRequestIndex] : undefined; - const docFound = indexFound && actualResult.found === true; - if (docFound && !this.rawDocExistsInNamespace(actualResult, namespace)) { + const docFound = indexFound && actualResult?.found === true; + // @ts-expect-error MultiGetHit._source is optional + if (docFound && !this.rawDocExistsInNamespace(actualResult!, namespace)) { const { id, type } = object; return { tag: 'Left' as 'Left', @@ -441,7 +438,10 @@ export class SavedObjectsRepository { }; } savedObjectNamespaces = - initialNamespaces || getSavedObjectNamespaces(namespace, docFound && actualResult); + initialNamespaces || + // @ts-expect-error MultiGetHit._source is optional + getSavedObjectNamespaces(namespace, docFound ? actualResult : undefined); + // @ts-expect-error MultiGetHit._source is optional versionProperties = getExpectedVersionProperties(version, actualResult); } else { if (this._registry.isSingleNamespace(object.type)) { @@ -500,7 +500,7 @@ export class SavedObjectsRepository { const { requestedId, rawMigratedDoc, esRequestIndex } = expectedResult.value; const { error, ...rawResponse } = Object.values( - bulkResponse?.body.items[esRequestIndex] + bulkResponse?.body.items[esRequestIndex] ?? {} )[0] as any; if (error) { @@ -564,10 +564,10 @@ export class SavedObjectsRepository { const bulkGetDocs = expectedBulkGetResults.filter(isRight).map(({ value: { type, id } }) => ({ _id: this._serializer.generateRawId(namespace, type, id), _index: this.getIndexForType(type), - _source: ['type', 'namespaces'], + _source: { includes: ['type', 'namespaces'] }, })); const bulkGetResponse = bulkGetDocs.length - ? await this.client.mget( + ? await this.client.mget( { body: { docs: bulkGetDocs, @@ -586,13 +586,14 @@ export class SavedObjectsRepository { const { type, id, esRequestIndex } = expectedResult.value; const doc = bulkGetResponse?.body.docs[esRequestIndex]; - if (doc.found) { + if (doc?.found) { errors.push({ id, type, error: { ...errorContent(SavedObjectsErrorHelpers.createConflictError(type, id)), - ...(!this.rawDocExistsInNamespace(doc, namespace) && { + // @ts-expect-error MultiGetHit._source is optional + ...(!this.rawDocExistsInNamespace(doc!, namespace) && { metadata: { isNotOverwritable: true }, }), }, @@ -636,7 +637,7 @@ export class SavedObjectsRepository { } } - const { body, statusCode } = await this.client.delete( + const { body, statusCode } = await this.client.delete( { id: rawId, index: this.getIndexForType(type), @@ -652,6 +653,7 @@ export class SavedObjectsRepository { } const deleteDocNotFound = body.result === 'not_found'; + // @ts-expect-error 'error' does not exist on type 'DeleteResponse' const deleteIndexNotFound = body.error && body.error.type === 'index_not_found_exception'; if (deleteDocNotFound || deleteIndexNotFound) { // see "404s from missing index" above @@ -813,15 +815,18 @@ export class SavedObjectsRepository { const esOptions = { // If `pit` is provided, we drop the `index`, otherwise ES returns 400. - ...(pit ? {} : { index: this.getIndicesForTypes(allowedTypes) }), + index: pit ? undefined : this.getIndicesForTypes(allowedTypes), // If `searchAfter` is provided, we drop `from` as it will not be used for pagination. - ...(searchAfter ? {} : { from: perPage * (page - 1) }), + from: searchAfter ? undefined : perPage * (page - 1), _source: includedFields(type, fields), preference, rest_total_hits_as_int: true, size: perPage, body: { + size: perPage, seq_no_primary_term: true, + from: perPage * (page - 1), + _source: includedFields(type, fields), ...getSearchDsl(this._mappings, this._registry, { search, defaultSearchOperator, @@ -841,7 +846,7 @@ export class SavedObjectsRepository { }, }; - const { body, statusCode } = await this.client.search>(esOptions, { + const { body, statusCode } = await this.client.search(esOptions, { ignore: [404], }); if (statusCode === 404) { @@ -860,13 +865,15 @@ export class SavedObjectsRepository { per_page: perPage, total: body.hits.total, saved_objects: body.hits.hits.map( - (hit: SavedObjectsRawDoc): SavedObjectsFindResult => ({ + (hit: estypes.Hit): SavedObjectsFindResult => ({ + // @ts-expect-error @elastic/elasticsearch declared Id as string | number ...this._rawToSavedObject(hit), - score: (hit as any)._score, - ...((hit as any).sort && { sort: (hit as any).sort }), + score: hit._score!, + // @ts-expect-error @elastic/elasticsearch declared sort as string | number + sort: hit.sort, }) ), - ...(body.pit_id && { pit_id: body.pit_id }), + pit_id: body.pit_id, } as SavedObjectsFindResponse; } @@ -925,10 +932,10 @@ export class SavedObjectsRepository { .map(({ value: { type, id, fields } }) => ({ _id: this._serializer.generateRawId(namespace, type, id), _index: this.getIndexForType(type), - _source: includedFields(type, fields), + _source: { includes: includedFields(type, fields) }, })); const bulkGetResponse = bulkGetDocs.length - ? await this.client.mget( + ? await this.client.mget( { body: { docs: bulkGetDocs, @@ -947,7 +954,8 @@ export class SavedObjectsRepository { const { type, id, esRequestIndex } = expectedResult.value; const doc = bulkGetResponse?.body.docs[esRequestIndex]; - if (!doc.found || !this.rawDocExistsInNamespace(doc, namespace)) { + // @ts-expect-error MultiGetHit._source is optional + if (!doc?.found || !this.rawDocExistsInNamespace(doc, namespace)) { return ({ id, type, @@ -955,6 +963,7 @@ export class SavedObjectsRepository { } as any) as SavedObject; } + // @ts-expect-error MultiGetHit._source is optional return this.getSavedObjectFromSource(type, id, doc); }), }; @@ -980,7 +989,7 @@ export class SavedObjectsRepository { const namespace = normalizeNamespace(options.namespace); - const { body, statusCode } = await this.client.get>( + const { body, statusCode } = await this.client.get( { id: this._serializer.generateRawId(namespace, type, id), index: this.getIndexForType(type), @@ -988,9 +997,13 @@ export class SavedObjectsRepository { { ignore: [404] } ); - const docNotFound = body.found === false; const indexNotFound = statusCode === 404; - if (docNotFound || indexNotFound || !this.rawDocExistsInNamespace(body, namespace)) { + + if ( + !isFoundGetResponse(body) || + indexNotFound || + !this.rawDocExistsInNamespace(body, namespace) + ) { // see "404s from missing index" above throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); } @@ -1026,7 +1039,7 @@ export class SavedObjectsRepository { const time = this._getCurrentTime(); // retrieve the alias, and if it is not disabled, update it - const aliasResponse = await this.client.update( + const aliasResponse = await this.client.update<{ 'legacy-url-alias': LegacyUrlAlias }>( { id: rawAliasId, index: this.getIndexForType(LEGACY_URL_ALIAS_TYPE), @@ -1059,15 +1072,16 @@ export class SavedObjectsRepository { if ( aliasResponse.statusCode === 404 || - aliasResponse.body.get.found === false || - aliasResponse.body.get._source[LEGACY_URL_ALIAS_TYPE]?.disabled === true + aliasResponse.body.get?.found === false || + aliasResponse.body.get?._source[LEGACY_URL_ALIAS_TYPE]?.disabled === true ) { // no legacy URL alias exists, or one exists but it's disabled; just attempt to get the object return this.resolveExactMatch(type, id, options); } - const legacyUrlAlias: LegacyUrlAlias = aliasResponse.body.get._source[LEGACY_URL_ALIAS_TYPE]; + + const legacyUrlAlias: LegacyUrlAlias = aliasResponse.body.get!._source[LEGACY_URL_ALIAS_TYPE]; const objectIndex = this.getIndexForType(type); - const bulkGetResponse = await this.client.mget( + const bulkGetResponse = await this.client.mget( { body: { docs: [ @@ -1090,23 +1104,28 @@ export class SavedObjectsRepository { const exactMatchDoc = bulkGetResponse?.body.docs[0]; const aliasMatchDoc = bulkGetResponse?.body.docs[1]; const foundExactMatch = + // @ts-expect-error MultiGetHit._source is optional exactMatchDoc.found && this.rawDocExistsInNamespace(exactMatchDoc, namespace); const foundAliasMatch = + // @ts-expect-error MultiGetHit._source is optional aliasMatchDoc.found && this.rawDocExistsInNamespace(aliasMatchDoc, namespace); if (foundExactMatch && foundAliasMatch) { return { + // @ts-expect-error MultiGetHit._source is optional saved_object: this.getSavedObjectFromSource(type, id, exactMatchDoc), outcome: 'conflict', aliasTargetId: legacyUrlAlias.targetId, }; } else if (foundExactMatch) { return { + // @ts-expect-error MultiGetHit._source is optional saved_object: this.getSavedObjectFromSource(type, id, exactMatchDoc), outcome: 'exactMatch', }; } else if (foundAliasMatch) { return { + // @ts-expect-error MultiGetHit._source is optional saved_object: this.getSavedObjectFromSource(type, legacyUrlAlias.targetId, aliasMatchDoc), outcome: 'aliasMatch', aliasTargetId: legacyUrlAlias.targetId, @@ -1153,7 +1172,7 @@ export class SavedObjectsRepository { }; const { body } = await this.client - .update({ + .update({ id: this._serializer.generateRawId(namespace, type, id), index: this.getIndexForType(type), ...getExpectedVersionProperties(version, preflightResult), @@ -1173,11 +1192,11 @@ export class SavedObjectsRepository { throw err; }); - const { originId } = body.get._source; - let namespaces = []; + const { originId } = body.get?._source ?? {}; + let namespaces: string[] = []; if (!this._registry.isNamespaceAgnostic(type)) { - namespaces = body.get._source.namespaces ?? [ - SavedObjectsUtils.namespaceIdToString(body.get._source.namespace), + namespaces = body.get?._source.namespaces ?? [ + SavedObjectsUtils.namespaceIdToString(body.get?._source.namespace), ]; } @@ -1185,7 +1204,6 @@ export class SavedObjectsRepository { id, type, updated_at: time, - // @ts-expect-error update doesn't have _seq_no, _primary_term as Record / any in LP version: encodeHitVersion(body), namespaces, ...(originId && { originId }), @@ -1325,7 +1343,7 @@ export class SavedObjectsRepository { return { namespaces: doc.namespaces }; } else { // if there are no namespaces remaining, delete the saved object - const { body, statusCode } = await this.client.delete( + const { body, statusCode } = await this.client.delete( { id: this._serializer.generateRawId(undefined, type, id), refresh, @@ -1343,6 +1361,7 @@ export class SavedObjectsRepository { } const deleteDocNotFound = body.result === 'not_found'; + // @ts-expect-error const deleteIndexNotFound = body.error && body.error.type === 'index_not_found_exception'; if (deleteDocNotFound || deleteIndexNotFound) { // see "404s from missing index" above @@ -1477,9 +1496,10 @@ export class SavedObjectsRepository { if (esRequestIndex !== undefined) { const indexFound = bulkGetResponse?.statusCode !== 404; const actualResult = indexFound ? bulkGetResponse?.body.docs[esRequestIndex] : undefined; - const docFound = indexFound && actualResult.found === true; + const docFound = indexFound && actualResult?.found === true; if ( !docFound || + // @ts-expect-error MultiGetHit is incorrectly missing _id, _source !this.rawDocExistsInNamespace(actualResult, getNamespaceId(objectNamespace)) ) { return { @@ -1491,10 +1511,13 @@ export class SavedObjectsRepository { }, }; } - namespaces = actualResult._source.namespaces ?? [ - SavedObjectsUtils.namespaceIdToString(actualResult._source.namespace), + // @ts-expect-error MultiGetHit is incorrectly missing _id, _source + namespaces = actualResult!._source.namespaces ?? [ + // @ts-expect-error MultiGetHit is incorrectly missing _id, _source + SavedObjectsUtils.namespaceIdToString(actualResult!._source.namespace), ]; - versionProperties = getExpectedVersionProperties(version, actualResult); + // @ts-expect-error MultiGetHit is incorrectly missing _id, _source + versionProperties = getExpectedVersionProperties(version, actualResult!); } else { if (this._registry.isSingleNamespace(type)) { // if `objectNamespace` is undefined, fall back to `options.namespace` @@ -1543,7 +1566,7 @@ export class SavedObjectsRepository { } const { type, id, namespaces, documentToSave, esRequestIndex } = expectedResult.value; - const response = bulkUpdateResponse?.body.items[esRequestIndex]; + const response = bulkUpdateResponse?.body.items[esRequestIndex] ?? {}; // When a bulk update operation is completed, any fields specified in `_sourceIncludes` will be found in the "get" value of the // returned object. We need to retrieve the `originId` if it exists so we can return it to the consumer. const { error, _seq_no: seqNo, _primary_term: primaryTerm, get } = Object.values( @@ -1636,7 +1659,7 @@ export class SavedObjectsRepository { } return { - updated: body.updated, + updated: body.updated!, }; } @@ -1745,7 +1768,7 @@ export class SavedObjectsRepository { const raw = this._serializer.savedObjectToRaw(migrated as SavedObjectSanitizedDoc); - const { body } = await this.client.update({ + const { body } = await this.client.update({ id: raw._id, index: this.getIndexForType(type), refresh, @@ -1783,17 +1806,16 @@ export class SavedObjectsRepository { }, }); - const { originId } = body.get._source; + const { originId } = body.get?._source ?? {}; return { id, type, ...(savedObjectNamespaces && { namespaces: savedObjectNamespaces }), ...(originId && { originId }), updated_at: time, - references: body.get._source.references, - // @ts-expect-error + references: body.get?._source.references ?? [], version: encodeHitVersion(body), - attributes: body.get._source[type], + attributes: body.get?._source[type], }; } @@ -1852,9 +1874,13 @@ export class SavedObjectsRepository { const { body, statusCode, - } = await this.client.openPointInTime(esOptions, { - ignore: [404], - }); + } = await this.client.openPointInTime( + // @ts-expect-error @elastic/elasticsearch OpenPointInTimeRequest.index expected to accept string[] + esOptions, + { + ignore: [404], + } + ); if (statusCode === 404) { throw SavedObjectsErrorHelpers.createGenericNotFoundError(); } @@ -1912,6 +1938,7 @@ export class SavedObjectsRepository { const { body } = await this.client.closePointInTime({ body: { id }, }); + return body; } @@ -2054,7 +2081,7 @@ export class SavedObjectsRepository { throw new Error(`Cannot make preflight get request for non-multi-namespace type '${type}'.`); } - const { body, statusCode } = await this.client.get>( + const { body, statusCode } = await this.client.get( { id: this._serializer.generateRawId(undefined, type, id), index: this.getIndexForType(type), @@ -2065,8 +2092,7 @@ export class SavedObjectsRepository { ); const indexFound = statusCode !== 404; - const docFound = indexFound && body.found === true; - if (docFound) { + if (indexFound && isFoundGetResponse(body)) { if (!this.rawDocExistsInNamespace(body, namespace)) { throw SavedObjectsErrorHelpers.createConflictError(type, id); } @@ -2091,7 +2117,7 @@ export class SavedObjectsRepository { } const rawId = this._serializer.generateRawId(undefined, type, id); - const { body, statusCode } = await this.client.get>( + const { body, statusCode } = await this.client.get( { id: rawId, index: this.getIndexForType(type), @@ -2100,17 +2126,20 @@ export class SavedObjectsRepository { ); const indexFound = statusCode !== 404; - const docFound = indexFound && body.found === true; - if (!docFound || !this.rawDocExistsInNamespace(body, namespace)) { + if ( + !indexFound || + !isFoundGetResponse(body) || + !this.rawDocExistsInNamespace(body, namespace) + ) { throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); } - return body as SavedObjectsRawDoc; + return body; } private getSavedObjectFromSource( type: string, id: string, - doc: { _seq_no: number; _primary_term: number; _source: SavedObjectsRawDocSource } + doc: { _seq_no?: number; _primary_term?: number; _source: SavedObjectsRawDocSource } ): SavedObject { const { originId, updated_at: updatedAt } = doc._source; @@ -2220,3 +2249,15 @@ const normalizeNamespace = (namespace?: string) => { const errorContent = (error: DecoratedError) => error.output.payload; const unique = (array: string[]) => [...new Set(array)]; + +/** + * Type and type guard function for converting a possibly not existant doc to an existant doc. + */ +type GetResponseFound = estypes.GetResponse & + Required< + Pick, '_primary_term' | '_seq_no' | '_version' | '_source'> + >; + +const isFoundGetResponse = ( + doc: estypes.GetResponse +): doc is GetResponseFound => doc.found; diff --git a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.test.ts b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.test.ts index 267d671361184e..b15560b82ab317 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.test.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.test.ts @@ -86,7 +86,7 @@ describe('getSearchDsl', () => { const opts = { type: 'foo', sortField: 'bar', - sortOrder: 'baz', + sortOrder: 'asc' as const, pit: { id: 'abc123' }, }; @@ -109,10 +109,10 @@ describe('getSearchDsl', () => { it('returns searchAfter if provided', () => { getQueryParams.mockReturnValue({ a: 'a' }); getSortingParams.mockReturnValue({ b: 'b' }); - expect(getSearchDsl(mappings, registry, { type: 'foo', searchAfter: [1, 'bar'] })).toEqual({ + expect(getSearchDsl(mappings, registry, { type: 'foo', searchAfter: ['1', 'bar'] })).toEqual({ a: 'a', b: 'b', - search_after: [1, 'bar'], + search_after: ['1', 'bar'], }); }); @@ -123,14 +123,14 @@ describe('getSearchDsl', () => { expect( getSearchDsl(mappings, registry, { type: 'foo', - searchAfter: [1, 'bar'], + searchAfter: ['1', 'bar'], pit: { id: 'abc123' }, }) ).toEqual({ a: 'a', b: 'b', pit: { id: 'abc123' }, - search_after: [1, 'bar'], + search_after: ['1', 'bar'], }); }); }); diff --git a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts index 9820544f02bd12..64b3dd428fb8bf 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts @@ -8,6 +8,7 @@ import Boom from '@hapi/boom'; +import type { estypes } from '@elastic/elasticsearch'; import { IndexMapping } from '../../../mappings'; import { SavedObjectsPitParams } from '../../../types'; import { getQueryParams, HasReferenceQueryParams, SearchOperator } from './query_params'; @@ -23,9 +24,9 @@ interface GetSearchDslOptions { defaultSearchOperator?: SearchOperator; searchFields?: string[]; rootSearchFields?: string[]; - searchAfter?: unknown[]; + searchAfter?: estypes.Id[]; sortField?: string; - sortOrder?: string; + sortOrder?: estypes.SortOrder; namespaces?: string[]; pit?: SavedObjectsPitParams; typeToNamespacesMap?: Map; @@ -80,6 +81,6 @@ export function getSearchDsl( }), ...getSortingParams(mappings, type, sortField, sortOrder), ...(pit ? getPitParams(pit) : {}), - ...(searchAfter ? { search_after: searchAfter } : {}), + search_after: searchAfter, }; } diff --git a/src/core/server/saved_objects/service/lib/search_dsl/sorting_params.ts b/src/core/server/saved_objects/service/lib/search_dsl/sorting_params.ts index e3bfba6a80f59d..64849c308f3f09 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/sorting_params.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/sorting_params.ts @@ -6,6 +6,7 @@ * Side Public License, v 1. */ +import type { estypes } from '@elastic/elasticsearch'; import Boom from '@hapi/boom'; import { getProperty, IndexMapping } from '../../../mappings'; @@ -15,8 +16,8 @@ export function getSortingParams( mappings: IndexMapping, type: string | string[], sortField?: string, - sortOrder?: string -) { + sortOrder?: estypes.SortOrder +): { sort?: estypes.SortContainer[] } { if (!sortField) { return {}; } diff --git a/src/core/server/saved_objects/service/saved_objects_client.ts b/src/core/server/saved_objects/service/saved_objects_client.ts index 9fa2896b7bbfe9..9a0ccb88d35558 100644 --- a/src/core/server/saved_objects/service/saved_objects_client.ts +++ b/src/core/server/saved_objects/service/saved_objects_client.ts @@ -162,7 +162,7 @@ export interface SavedObjectsFindResult extends SavedObject { * await savedObjectsClient.closePointInTime(page2.pit_id); * ``` */ - sort?: unknown[]; + sort?: string[]; } /** diff --git a/src/core/server/saved_objects/types.ts b/src/core/server/saved_objects/types.ts index 11a694c72f29ff..ecda120e025d89 100644 --- a/src/core/server/saved_objects/types.ts +++ b/src/core/server/saved_objects/types.ts @@ -6,6 +6,7 @@ * Side Public License, v 1. */ +import type { estypes } from '@elastic/elasticsearch'; import { SavedObjectsClient } from './service/saved_objects_client'; import { SavedObjectsTypeMappingDefinition } from './mappings'; import { SavedObjectMigrationMap } from './migrations'; @@ -79,7 +80,7 @@ export interface SavedObjectsFindOptions { page?: number; perPage?: number; sortField?: string; - sortOrder?: string; + sortOrder?: estypes.SortOrder; /** * An array of fields to include in the results * @example @@ -93,7 +94,7 @@ export interface SavedObjectsFindOptions { /** * Use the sort values from the previous page to retrieve the next page of results. */ - searchAfter?: unknown[]; + searchAfter?: estypes.Id[]; /** * The fields to perform the parsed query against. Unlike the `searchFields` argument, these are expected to be root fields and will not * be modified. If used in conjunction with `searchFields`, both are concatenated together. diff --git a/src/core/server/saved_objects/version/encode_hit_version.ts b/src/core/server/saved_objects/version/encode_hit_version.ts index 614666c6e1da65..979df93dc57b56 100644 --- a/src/core/server/saved_objects/version/encode_hit_version.ts +++ b/src/core/server/saved_objects/version/encode_hit_version.ts @@ -12,6 +12,6 @@ import { encodeVersion } from './encode_version'; * Helper for encoding a version from a "hit" (hits.hits[#] from _search) or * "doc" (body from GET, update, etc) object */ -export function encodeHitVersion(response: { _seq_no: number; _primary_term: number }) { +export function encodeHitVersion(response: { _seq_no?: number; _primary_term?: number }) { return encodeVersion(response._seq_no, response._primary_term); } diff --git a/src/core/server/saved_objects/version/encode_version.ts b/src/core/server/saved_objects/version/encode_version.ts index fa778ee931e417..9c0b0a7428f389 100644 --- a/src/core/server/saved_objects/version/encode_version.ts +++ b/src/core/server/saved_objects/version/encode_version.ts @@ -13,7 +13,7 @@ import { encodeBase64 } from './base64'; * that can be used in the saved object API in place of numeric * version numbers */ -export function encodeVersion(seqNo: number, primaryTerm: number) { +export function encodeVersion(seqNo?: number, primaryTerm?: number) { if (!Number.isInteger(primaryTerm)) { throw new TypeError('_primary_term from elasticsearch must be an integer'); } diff --git a/src/core/server/server.api.md b/src/core/server/server.api.md index 3d2023108c46a0..73f8a440751620 100644 --- a/src/core/server/server.api.md +++ b/src/core/server/server.api.md @@ -50,6 +50,7 @@ import { DetailedPeerCertificate } from 'tls'; import { Duration } from 'moment'; import { Duration as Duration_2 } from 'moment-timezone'; import { EnvironmentMode } from '@kbn/config'; +import { estypes } from '@elastic/elasticsearch'; import { ExistsParams } from 'elasticsearch'; import { ExplainParams } from 'elasticsearch'; import { FieldStatsParams } from 'elasticsearch'; @@ -2507,12 +2508,12 @@ export interface SavedObjectsFindOptions { preference?: string; rootSearchFields?: string[]; search?: string; - searchAfter?: unknown[]; + searchAfter?: estypes.Id[]; searchFields?: string[]; // (undocumented) sortField?: string; // (undocumented) - sortOrder?: string; + sortOrder?: estypes.SortOrder; // (undocumented) type: string | string[]; typeToNamespacesMap?: Map; @@ -2543,7 +2544,7 @@ export interface SavedObjectsFindResponse { // @public (undocumented) export interface SavedObjectsFindResult extends SavedObject { score: number; - sort?: unknown[]; + sort?: string[]; } // @public diff --git a/src/plugins/data/common/search/es_search/types.ts b/src/plugins/data/common/search/es_search/types.ts index dc1de8d1338f1e..12dc0c1b2599d7 100644 --- a/src/plugins/data/common/search/es_search/types.ts +++ b/src/plugins/data/common/search/es_search/types.ts @@ -5,19 +5,18 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ +import type { estypes } from '@elastic/elasticsearch'; -import { SearchResponse } from 'elasticsearch'; -import { Search } from '@elastic/elasticsearch/api/requestParams'; import { IKibanaSearchRequest, IKibanaSearchResponse } from '../types'; export const ES_SEARCH_STRATEGY = 'es'; -export type ISearchRequestParams> = { +export type ISearchRequestParams = { trackTotalHits?: boolean; -} & Search; +} & estypes.SearchRequest; export interface IEsSearchRequest extends IKibanaSearchRequest { indexType?: string; } -export type IEsSearchResponse = IKibanaSearchResponse>; +export type IEsSearchResponse = IKibanaSearchResponse>; diff --git a/src/plugins/data/common/search/expressions/utils/courier_inspector_stats.ts b/src/plugins/data/common/search/expressions/utils/courier_inspector_stats.ts index 6013b3d6c6f5f7..99acbce8935c4a 100644 --- a/src/plugins/data/common/search/expressions/utils/courier_inspector_stats.ts +++ b/src/plugins/data/common/search/expressions/utils/courier_inspector_stats.ts @@ -14,7 +14,7 @@ */ import { i18n } from '@kbn/i18n'; -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { ISearchSource } from 'src/plugins/data/public'; import { RequestStatistics } from 'src/plugins/inspector/common'; @@ -50,7 +50,7 @@ export function getRequestInspectorStats(searchSource: ISearchSource) { /** @public */ export function getResponseInspectorStats( - resp: SearchResponse, + resp: estypes.SearchResponse, searchSource?: ISearchSource ) { const lastRequest = diff --git a/src/plugins/data/common/search/search_source/fetch/request_error.ts b/src/plugins/data/common/search/search_source/fetch/request_error.ts index 14185d7d5afd38..d8c750d011b039 100644 --- a/src/plugins/data/common/search/search_source/fetch/request_error.ts +++ b/src/plugins/data/common/search/search_source/fetch/request_error.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { KbnError } from '../../../../../kibana_utils/common'; import { SearchError } from './types'; @@ -16,8 +16,8 @@ import { SearchError } from './types'; * @param {Object} resp - optional HTTP response */ export class RequestFailure extends KbnError { - public resp?: SearchResponse; - constructor(err: SearchError | null = null, resp?: SearchResponse) { + public resp?: estypes.SearchResponse; + constructor(err: SearchError | null = null, resp?: estypes.SearchResponse) { super(`Request to Elasticsearch failed: ${JSON.stringify(resp || err?.message)}`); this.resp = resp; diff --git a/src/plugins/data/common/search/search_source/fetch/types.ts b/src/plugins/data/common/search/search_source/fetch/types.ts index 2387d9dbffa3a7..8e8a9f1025b80e 100644 --- a/src/plugins/data/common/search/search_source/fetch/types.ts +++ b/src/plugins/data/common/search/search_source/fetch/types.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { LegacyFetchHandlers } from '../legacy/types'; import { GetConfigFn } from '../../../types'; @@ -25,7 +25,10 @@ export interface FetchHandlers { * Callback which can be used to hook into responses, modify them, or perform * side effects like displaying UI errors on the client. */ - onResponse: (request: SearchRequest, response: SearchResponse) => SearchResponse; + onResponse: ( + request: SearchRequest, + response: estypes.SearchResponse + ) => estypes.SearchResponse; /** * These handlers are only used by the legacy defaultSearchStrategy and can be removed * once that strategy has been deprecated. diff --git a/src/plugins/data/common/search/search_source/legacy/call_client.ts b/src/plugins/data/common/search/search_source/legacy/call_client.ts index a288cdc22c576d..4c1156aac70153 100644 --- a/src/plugins/data/common/search/search_source/legacy/call_client.ts +++ b/src/plugins/data/common/search/search_source/legacy/call_client.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { FetchHandlers, SearchRequest } from '../fetch'; import { defaultSearchStrategy } from './default_search_strategy'; import { ISearchOptions } from '../../index'; @@ -21,7 +21,7 @@ export function callClient( [SearchRequest, ISearchOptions] > = searchRequests.map((request, i) => [request, requestsOptions[i]]); const requestOptionsMap = new Map(requestOptionEntries); - const requestResponseMap = new Map>>(); + const requestResponseMap = new Map>>(); const { searching, abort } = defaultSearchStrategy.search({ searchRequests, diff --git a/src/plugins/data/common/search/search_source/legacy/fetch_soon.ts b/src/plugins/data/common/search/search_source/legacy/fetch_soon.ts index e42ef6617594ab..ff8ae2d19bd56b 100644 --- a/src/plugins/data/common/search/search_source/legacy/fetch_soon.ts +++ b/src/plugins/data/common/search/search_source/legacy/fetch_soon.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { UI_SETTINGS } from '../../../constants'; import { FetchHandlers, SearchRequest } from '../fetch'; import { ISearchOptions } from '../../index'; @@ -57,9 +57,11 @@ async function delayedFetch( options: ISearchOptions, fetchHandlers: FetchHandlers, ms: number -): Promise> { +): Promise> { if (ms === 0) { - return callClient([request], [options], fetchHandlers)[0]; + return callClient([request], [options], fetchHandlers)[0] as Promise< + estypes.SearchResponse + >; } const i = requestsToFetch.length; diff --git a/src/plugins/data/common/search/search_source/legacy/types.ts b/src/plugins/data/common/search/search_source/legacy/types.ts index 5a60d1082b0ed4..a4328528fd6625 100644 --- a/src/plugins/data/common/search/search_source/legacy/types.ts +++ b/src/plugins/data/common/search/search_source/legacy/types.ts @@ -7,8 +7,7 @@ */ import { BehaviorSubject } from 'rxjs'; -import { ApiResponse } from '@elastic/elasticsearch'; -import { SearchResponse } from 'elasticsearch'; +import type { estypes, ApiResponse } from '@elastic/elasticsearch'; import { FetchHandlers, SearchRequest } from '../fetch'; interface MsearchHeaders { @@ -28,7 +27,7 @@ export interface MsearchRequestBody { // @internal export interface MsearchResponse { - body: ApiResponse<{ responses: Array> }>; + body: ApiResponse<{ responses: Array> }>; } // @internal @@ -51,6 +50,6 @@ export interface SearchStrategyProvider { } export interface SearchStrategyResponse { - searching: Promise>>; + searching: Promise>>; abort: () => void; } diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index 65f1c67db80d51..7215d71bbc92a5 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -28,6 +28,7 @@ import { DetailedPeerCertificate } from 'tls'; import { Ensure } from '@kbn/utility-types'; import { EnvironmentMode } from '@kbn/config'; import { ErrorToastOptions } from 'src/core/public/notifications'; +import { estypes } from '@elastic/elasticsearch'; import { EuiBreadcrumb } from '@elastic/eui'; import { EuiButtonEmptyProps } from '@elastic/eui'; import { EuiComboBoxProps } from '@elastic/eui'; @@ -92,8 +93,6 @@ import { SavedObjectsFindOptions } from 'kibana/public'; import { SavedObjectsFindResponse } from 'kibana/server'; import { SavedObjectsUpdateResponse } from 'kibana/server'; import { SchemaTypeError } from '@kbn/config-schema'; -import { Search } from '@elastic/elasticsearch/api/requestParams'; -import { SearchResponse } from 'elasticsearch'; import { SerializedFieldFormat as SerializedFieldFormat_2 } from 'src/plugins/expressions/common'; import { StartServicesAccessor } from 'kibana/public'; import { ToastInputFields } from 'src/core/public/notifications'; @@ -1128,7 +1127,7 @@ export interface IEsSearchRequest extends IKibanaSearchRequest = IKibanaSearchResponse>; +export type IEsSearchResponse = IKibanaSearchResponse>; // Warning: (ae-missing-release-tag) "IFieldFormat" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // @@ -2423,9 +2422,9 @@ export class SearchSource { createChild(options?: {}): SearchSource; createCopy(): SearchSource; destroy(): void; - fetch$(options?: ISearchOptions): import("rxjs").Observable>; + fetch$(options?: ISearchOptions): import("rxjs").Observable>; // @deprecated - fetch(options?: ISearchOptions): Promise>; + fetch(options?: ISearchOptions): Promise>; getField(field: K, recurse?: boolean): SearchSourceFields[K]; getFields(): SearchSourceFields; getId(): string; diff --git a/src/plugins/data/public/search/expressions/es_raw_response.ts b/src/plugins/data/public/search/expressions/es_raw_response.ts index 6b44a7afb6d673..2d12af017d88c5 100644 --- a/src/plugins/data/public/search/expressions/es_raw_response.ts +++ b/src/plugins/data/public/search/expressions/es_raw_response.ts @@ -6,14 +6,14 @@ * Side Public License, v 1. */ -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { ExpressionTypeDefinition } from '../../../../expressions/common'; const name = 'es_raw_response'; export interface EsRawResponse { type: typeof name; - body: SearchResponse; + body: estypes.SearchResponse; } // flattens elasticsearch object into table rows @@ -46,11 +46,11 @@ function flatten(obj: any, keyPrefix = '') { } } -const parseRawDocs = (hits: SearchResponse['hits']) => { +const parseRawDocs = (hits: estypes.SearchResponse['hits']) => { return hits.hits.map((hit) => hit.fields || hit._source).filter((hit) => hit); }; -const convertResult = (body: SearchResponse) => { +const convertResult = (body: estypes.SearchResponse) => { return !body.aggregations ? parseRawDocs(body.hits) : flatten(body.aggregations); }; diff --git a/src/plugins/data/public/search/fetch/handle_response.tsx b/src/plugins/data/public/search/fetch/handle_response.tsx index 00d5b11089d62c..57ee5737e50a29 100644 --- a/src/plugins/data/public/search/fetch/handle_response.tsx +++ b/src/plugins/data/public/search/fetch/handle_response.tsx @@ -9,13 +9,13 @@ import React from 'react'; import { i18n } from '@kbn/i18n'; import { EuiSpacer } from '@elastic/eui'; -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { ShardFailureOpenModalButton } from '../../ui/shard_failure_modal'; import { toMountPoint } from '../../../../kibana_react/public'; import { getNotifications } from '../../services'; import { SearchRequest } from '..'; -export function handleResponse(request: SearchRequest, response: SearchResponse) { +export function handleResponse(request: SearchRequest, response: estypes.SearchResponse) { if (response.timed_out) { getNotifications().toasts.addWarning({ title: i18n.translate('data.search.searchSource.fetch.requestTimedOutNotificationMessage', { diff --git a/src/plugins/data/public/ui/shard_failure_modal/shard_failure_modal.tsx b/src/plugins/data/public/ui/shard_failure_modal/shard_failure_modal.tsx index f510420cb30e8a..8e6ad4bc92c8f3 100644 --- a/src/plugins/data/public/ui/shard_failure_modal/shard_failure_modal.tsx +++ b/src/plugins/data/public/ui/shard_failure_modal/shard_failure_modal.tsx @@ -21,14 +21,14 @@ import { EuiButtonEmpty, EuiCallOut, } from '@elastic/eui'; -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { ShardFailureTable } from './shard_failure_table'; import { ShardFailureRequest } from './shard_failure_types'; export interface Props { onClose: () => void; request: ShardFailureRequest; - response: SearchResponse; + response: estypes.SearchResponse; title: string; } diff --git a/src/plugins/data/public/ui/shard_failure_modal/shard_failure_open_modal_button.tsx b/src/plugins/data/public/ui/shard_failure_modal/shard_failure_open_modal_button.tsx index 0907d6607579f9..a230378d6c3d3c 100644 --- a/src/plugins/data/public/ui/shard_failure_modal/shard_failure_open_modal_button.tsx +++ b/src/plugins/data/public/ui/shard_failure_modal/shard_failure_open_modal_button.tsx @@ -9,8 +9,8 @@ import React from 'react'; import { FormattedMessage } from '@kbn/i18n/react'; import { EuiButton, EuiTextAlign } from '@elastic/eui'; +import type { estypes } from '@elastic/elasticsearch'; -import { SearchResponse } from 'elasticsearch'; import { getOverlays } from '../../services'; import { toMountPoint } from '../../../../kibana_react/public'; import { ShardFailureModal } from './shard_failure_modal'; @@ -19,7 +19,7 @@ import { ShardFailureRequest } from './shard_failure_types'; // @internal export interface ShardFailureOpenModalButtonProps { request: ShardFailureRequest; - response: SearchResponse; + response: estypes.SearchResponse; title: string; } diff --git a/src/plugins/data/server/autocomplete/value_suggestions_route.ts b/src/plugins/data/server/autocomplete/value_suggestions_route.ts index 8e6d3afa18ed5f..75a41d50ac7bc4 100644 --- a/src/plugins/data/server/autocomplete/value_suggestions_route.ts +++ b/src/plugins/data/server/autocomplete/value_suggestions_route.ts @@ -12,7 +12,8 @@ import { IRouter, SharedGlobalConfig } from 'kibana/server'; import { Observable } from 'rxjs'; import { first } from 'rxjs/operators'; -import { IFieldType, Filter, ES_SEARCH_STRATEGY, IEsSearchRequest } from '../index'; +import type { estypes } from '@elastic/elasticsearch'; +import { IFieldType, ES_SEARCH_STRATEGY, IEsSearchRequest } from '../index'; import { getRequestAbortedSignal } from '../lib'; import { DataRequestHandlerContext } from '../types'; @@ -89,7 +90,7 @@ async function getBody( { timeout, terminate_after }: Record, field: IFieldType | string, query: string, - filters: Filter[] = [] + filters: estypes.QueryContainer[] = [] ) { const isFieldObject = (f: any): f is IFieldType => Boolean(f && f.name); @@ -98,7 +99,7 @@ async function getBody( q.replace(/[.?+*|{}[\]()"\\#@&<>~]/g, (match) => `\\${match}`); // Helps ensure that the regex is not evaluated eagerly against the terms dictionary - const executionHint = 'map'; + const executionHint = 'map' as const; // We don't care about the accuracy of the counts, just the content of the terms, so this reduces // the amount of information that needs to be transmitted to the coordinating node diff --git a/src/plugins/data/server/index_patterns/fetcher/lib/es_api.ts b/src/plugins/data/server/index_patterns/fetcher/lib/es_api.ts index db950e7aa48f94..69a9280dd93d88 100644 --- a/src/plugins/data/server/index_patterns/fetcher/lib/es_api.ts +++ b/src/plugins/data/server/index_patterns/fetcher/lib/es_api.ts @@ -8,17 +8,6 @@ import { ElasticsearchClient } from 'kibana/server'; import { convertEsError } from './errors'; -import { FieldCapsResponse } from './field_capabilities'; - -export interface IndicesAliasResponse { - [index: string]: IndexAliasResponse; -} - -export interface IndexAliasResponse { - aliases: { - [aliasName: string]: Record; - }; -} /** * Call the index.getAlias API for a list of indices. @@ -67,7 +56,7 @@ export async function callFieldCapsApi( fieldCapsOptions: { allow_no_indices: boolean } = { allow_no_indices: false } ) { try { - return await callCluster.fieldCaps({ + return await callCluster.fieldCaps({ index: indices, fields: '*', ignore_unavailable: true, diff --git a/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_caps_response.ts b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_caps_response.ts index 31fd60b0382aa7..c4c1ffa3cf9f91 100644 --- a/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_caps_response.ts +++ b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/field_caps_response.ts @@ -7,23 +7,11 @@ */ import { uniq } from 'lodash'; +import type { estypes } from '@elastic/elasticsearch'; import { castEsToKbnFieldTypeName } from '../../../../../common'; import { shouldReadFieldFromDocValues } from './should_read_field_from_doc_values'; import { FieldDescriptor } from '../../../fetcher'; -interface FieldCapObject { - type: string; - searchable: boolean; - aggregatable: boolean; - indices?: string[]; - non_searchable_indices?: string[]; - non_aggregatable_indices?: string[]; -} - -export interface FieldCapsResponse { - fields: Record>; -} - /** * Read the response from the _field_caps API to determine the type and * "aggregatable"/"searchable" status of each field. @@ -80,7 +68,9 @@ export interface FieldCapsResponse { * @param {FieldCapsResponse} fieldCapsResponse * @return {Array} */ -export function readFieldCapsResponse(fieldCapsResponse: FieldCapsResponse): FieldDescriptor[] { +export function readFieldCapsResponse( + fieldCapsResponse: estypes.FieldCapabilitiesResponse +): FieldDescriptor[] { const capsByNameThenType = fieldCapsResponse.fields; const kibanaFormattedCaps = Object.keys(capsByNameThenType).reduce<{ diff --git a/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/index.ts b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/index.ts index d7150d81e3803b..773a615727ee5f 100644 --- a/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/index.ts +++ b/src/plugins/data/server/index_patterns/fetcher/lib/field_capabilities/index.ts @@ -7,5 +7,4 @@ */ export { getFieldCapabilities } from './field_capabilities'; -export { FieldCapsResponse } from './field_caps_response'; export { shouldReadFieldFromDocValues } from './should_read_field_from_doc_values'; diff --git a/src/plugins/data/server/index_patterns/fetcher/lib/resolve_time_pattern.ts b/src/plugins/data/server/index_patterns/fetcher/lib/resolve_time_pattern.ts index d01f74429c3a53..32b9d8c7f893f3 100644 --- a/src/plugins/data/server/index_patterns/fetcher/lib/resolve_time_pattern.ts +++ b/src/plugins/data/server/index_patterns/fetcher/lib/resolve_time_pattern.ts @@ -12,7 +12,7 @@ import moment from 'moment'; import { ElasticsearchClient } from 'kibana/server'; import { timePatternToWildcard } from './time_pattern_to_wildcard'; -import { callIndexAliasApi, IndicesAliasResponse } from './es_api'; +import { callIndexAliasApi } from './es_api'; /** * Convert a time pattern into a list of indexes it could @@ -28,7 +28,7 @@ import { callIndexAliasApi, IndicesAliasResponse } from './es_api'; export async function resolveTimePattern(callCluster: ElasticsearchClient, timePattern: string) { const aliases = await callIndexAliasApi(callCluster, timePatternToWildcard(timePattern)); - const allIndexDetails = chain(aliases.body) + const allIndexDetails = chain(aliases.body) .reduce( (acc: string[], index: any, indexName: string) => acc.concat(indexName, Object.keys(index.aliases || {})), diff --git a/src/plugins/data/server/search/collectors/fetch.ts b/src/plugins/data/server/search/collectors/fetch.ts index 6dfc29e2cf2a66..aed35d73c7eb64 100644 --- a/src/plugins/data/server/search/collectors/fetch.ts +++ b/src/plugins/data/server/search/collectors/fetch.ts @@ -9,18 +9,16 @@ import { Observable } from 'rxjs'; import { first } from 'rxjs/operators'; import { SharedGlobalConfig } from 'kibana/server'; -import { SearchResponse } from 'elasticsearch'; import { CollectorFetchContext } from 'src/plugins/usage_collection/server'; import { CollectedUsage, ReportedUsage } from './register'; interface SearchTelemetry { 'search-telemetry': CollectedUsage; } -type ESResponse = SearchResponse; export function fetchProvider(config$: Observable) { return async ({ esClient }: CollectorFetchContext): Promise => { const config = await config$.pipe(first()).toPromise(); - const { body: esResponse } = await esClient.search( + const { body: esResponse } = await esClient.search( { index: config.kibana.index, body: { @@ -37,7 +35,7 @@ export function fetchProvider(config$: Observable) { averageDuration: null, }; } - const { successCount, errorCount, totalDuration } = esResponse.hits.hits[0]._source[ + const { successCount, errorCount, totalDuration } = esResponse.hits.hits[0]._source![ 'search-telemetry' ]; const averageDuration = totalDuration / successCount; diff --git a/src/plugins/data/server/search/es_search/es_search_strategy.ts b/src/plugins/data/server/search/es_search/es_search_strategy.ts index cc81dce94c4ec6..1afe6275452483 100644 --- a/src/plugins/data/server/search/es_search/es_search_strategy.ts +++ b/src/plugins/data/server/search/es_search/es_search_strategy.ts @@ -8,7 +8,6 @@ import { from, Observable } from 'rxjs'; import { first, tap } from 'rxjs/operators'; -import type { SearchResponse } from 'elasticsearch'; import type { Logger, SharedGlobalConfig } from 'kibana/server'; import type { ISearchStrategy } from '../types'; import type { SearchUsage } from '../collectors'; @@ -44,7 +43,7 @@ export const esSearchStrategyProvider = ( ...getShardTimeout(config), ...request.params, }; - const promise = esClient.asCurrentUser.search>(params); + const promise = esClient.asCurrentUser.search(params); const { body } = await shimAbortSignal(promise, abortSignal); const response = shimHitsTotal(body, options); return toKibanaSearchResponse(response); diff --git a/src/plugins/data/server/search/es_search/response_utils.ts b/src/plugins/data/server/search/es_search/response_utils.ts index 975ce392656b12..3bee63624ef673 100644 --- a/src/plugins/data/server/search/es_search/response_utils.ts +++ b/src/plugins/data/server/search/es_search/response_utils.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { ISearchOptions } from '../../../common'; /** @@ -14,7 +14,7 @@ import { ISearchOptions } from '../../../common'; * not included as it is already included in `successful`. * @internal */ -export function getTotalLoaded(response: SearchResponse) { +export function getTotalLoaded(response: estypes.SearchResponse) { const { total, failed, successful } = response._shards; const loaded = failed + successful; return { total, loaded }; @@ -24,7 +24,7 @@ export function getTotalLoaded(response: SearchResponse) { * Get the Kibana representation of this response (see `IKibanaSearchResponse`). * @internal */ -export function toKibanaSearchResponse(rawResponse: SearchResponse) { +export function toKibanaSearchResponse(rawResponse: estypes.SearchResponse) { return { rawResponse, isPartial: false, @@ -41,7 +41,7 @@ export function toKibanaSearchResponse(rawResponse: SearchResponse) { * @internal */ export function shimHitsTotal( - response: SearchResponse, + response: estypes.SearchResponse, { legacyHitsTotal = true }: ISearchOptions = {} ) { if (!legacyHitsTotal) return response; diff --git a/src/plugins/data/server/search/routes/call_msearch.ts b/src/plugins/data/server/search/routes/call_msearch.ts index 8648d3f4526fec..0c238adf831bd9 100644 --- a/src/plugins/data/server/search/routes/call_msearch.ts +++ b/src/plugins/data/server/search/routes/call_msearch.ts @@ -8,7 +8,6 @@ import { Observable } from 'rxjs'; import { first } from 'rxjs/operators'; -import { SearchResponse } from 'elasticsearch'; import { IUiSettingsClient, IScopedClusterClient, SharedGlobalConfig } from 'src/core/server'; import type { MsearchRequestBody, MsearchResponse } from '../../../common/search/search_source'; @@ -66,6 +65,7 @@ export function getCallMsearch(dependencies: CallMsearchDependencies) { try { const promise = esClient.asCurrentUser.msearch( { + // @ts-expect-error @elastic/elasticsearch client types don't support plain string bodies body: convertRequestBody(params.body, timeout), }, { @@ -78,9 +78,7 @@ export function getCallMsearch(dependencies: CallMsearchDependencies) { body: { ...response, body: { - responses: response.body.responses?.map((r: SearchResponse) => - shimHitsTotal(r) - ), + responses: response.body.responses?.map((r) => shimHitsTotal(r)), }, }, }; diff --git a/src/plugins/data/server/server.api.md b/src/plugins/data/server/server.api.md index 2ceeaddf3cd1ee..f5505f5b4f320e 100644 --- a/src/plugins/data/server/server.api.md +++ b/src/plugins/data/server/server.api.md @@ -25,6 +25,7 @@ import { ElasticsearchClient as ElasticsearchClient_2 } from 'kibana/server'; import { Ensure } from '@kbn/utility-types'; import { EnvironmentMode } from '@kbn/config'; import { ErrorToastOptions } from 'src/core/public/notifications'; +import { estypes } from '@elastic/elasticsearch'; import { ExecutionContext } from 'src/plugins/expressions/common'; import { ExpressionAstExpression } from 'src/plugins/expressions/common'; import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common'; @@ -64,7 +65,6 @@ import { SavedObjectsFindOptions } from 'kibana/server'; import { SavedObjectsFindResponse } from 'kibana/server'; import { SavedObjectsUpdateResponse } from 'kibana/server'; import { Search } from '@elastic/elasticsearch/api/requestParams'; -import { SearchResponse } from 'elasticsearch'; import { SerializedFieldFormat as SerializedFieldFormat_2 } from 'src/plugins/expressions/common'; import { SharedGlobalConfig as SharedGlobalConfig_2 } from 'kibana/server'; import { ToastInputFields } from 'src/core/public/notifications'; @@ -602,7 +602,7 @@ export function getTime(indexPattern: IIndexPattern | undefined, timeRange: Time }): import("../..").RangeFilter | undefined; // @internal -export function getTotalLoaded(response: SearchResponse): { +export function getTotalLoaded(response: estypes.SearchResponse): { total: number; loaded: number; }; @@ -637,7 +637,7 @@ export interface IEsSearchRequest extends IKibanaSearchRequest = IKibanaSearchResponse>; +export type IEsSearchResponse = IKibanaSearchResponse>; // Warning: (ae-missing-release-tag) "IFieldFormatsRegistry" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // @@ -1391,30 +1391,26 @@ export function searchUsageObserver(logger: Logger_2, usage?: SearchUsage, { isR export const shimAbortSignal: (promise: TransportRequestPromise, signal?: AbortSignal | undefined) => TransportRequestPromise; // @internal -export function shimHitsTotal(response: SearchResponse, { legacyHitsTotal }?: ISearchOptions): { +export function shimHitsTotal(response: estypes.SearchResponse, { legacyHitsTotal }?: ISearchOptions): { hits: { total: any; - max_score: number; - hits: { - _index: string; - _type: string; - _id: string; - _score: number; - _source: unknown; - _version?: number | undefined; - _explanation?: import("elasticsearch").Explanation | undefined; - fields?: any; - highlight?: any; - inner_hits?: any; - matched_queries?: string[] | undefined; - sort?: string[] | undefined; - }[]; + hits: estypes.Hit[]; + max_score?: number | undefined; }; took: number; timed_out: boolean; + _shards: estypes.ShardStatistics; + aggregations?: Record | undefined; + _clusters?: estypes.ClusterStatistics | undefined; + documents?: unknown[] | undefined; + fields?: Record | undefined; + max_score?: number | undefined; + num_reduce_phases?: number | undefined; + profile?: estypes.Profile | undefined; + pit_id?: string | undefined; _scroll_id?: string | undefined; - _shards: import("elasticsearch").ShardsResponse; - aggregations?: any; + suggest?: Record[]> | undefined; + terminated_early?: boolean | undefined; }; // Warning: (ae-missing-release-tag) "shouldReadFieldFromDocValues" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) @@ -1432,10 +1428,10 @@ export type TimeRange = { }; // @internal -export function toKibanaSearchResponse(rawResponse: SearchResponse): { +export function toKibanaSearchResponse(rawResponse: estypes.SearchResponse): { total: number; loaded: number; - rawResponse: SearchResponse; + rawResponse: estypes.SearchResponse; isPartial: boolean; isRunning: boolean; }; diff --git a/src/plugins/discover/public/application/angular/doc_table/create_doc_table_react.tsx b/src/plugins/discover/public/application/angular/doc_table/create_doc_table_react.tsx index e768b750aa134c..4dfd821abd4300 100644 --- a/src/plugins/discover/public/application/angular/doc_table/create_doc_table_react.tsx +++ b/src/plugins/discover/public/application/angular/doc_table/create_doc_table_react.tsx @@ -9,6 +9,7 @@ import angular, { auto, ICompileService, IScope } from 'angular'; import { render } from 'react-dom'; import React, { useRef, useEffect } from 'react'; +import type { estypes } from '@elastic/elasticsearch'; import { EuiButtonEmpty } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import { getServices, IIndexPattern } from '../../../kibana_services'; @@ -19,7 +20,7 @@ export interface DocTableLegacyProps { searchDescription?: string; searchTitle?: string; onFilter: (field: IndexPatternField | string, value: string, type: '+' | '-') => void; - rows: Array>; + rows: estypes.Hit[]; indexPattern: IIndexPattern; minimumVisibleRows: number; onAddColumn?: (column: string) => void; diff --git a/src/plugins/discover/public/application/components/context_app/context_app_legacy.tsx b/src/plugins/discover/public/application/components/context_app/context_app_legacy.tsx index 8dded3598c2793..5031f78c49fcca 100644 --- a/src/plugins/discover/public/application/components/context_app/context_app_legacy.tsx +++ b/src/plugins/discover/public/application/components/context_app/context_app_legacy.tsx @@ -87,6 +87,7 @@ export function ContextAppLegacy(renderProps: ContextAppProps) { minimumVisibleRows, useNewFieldsApi, } = renderProps; + // @ts-expect-error doesn't implement full DocTableLegacyProps interface return { columns, indexPattern, diff --git a/src/plugins/discover/public/application/components/discover_grid/discover_grid_flyout.tsx b/src/plugins/discover/public/application/components/discover_grid/discover_grid_flyout.tsx index 87b9c6243abd84..dbc94e5021294c 100644 --- a/src/plugins/discover/public/application/components/discover_grid/discover_grid_flyout.tsx +++ b/src/plugins/discover/public/application/components/discover_grid/discover_grid_flyout.tsx @@ -140,7 +140,7 @@ export function DiscoverGridFlyout({ iconType="documents" flush="left" href={getContextUrl( - hit._id, + String(hit._id), indexPattern.id, columns, services.filterManager, diff --git a/src/plugins/discover/public/application/components/discover_grid/get_render_cell_value.test.tsx b/src/plugins/discover/public/application/components/discover_grid/get_render_cell_value.test.tsx index f1025a0881d1f6..74cf083d82653f 100644 --- a/src/plugins/discover/public/application/components/discover_grid/get_render_cell_value.test.tsx +++ b/src/plugins/discover/public/application/components/discover_grid/get_render_cell_value.test.tsx @@ -10,6 +10,7 @@ import React from 'react'; import { ReactWrapper, shallow } from 'enzyme'; import { getRenderCellValueFn } from './get_render_cell_value'; import { indexPatternMock } from '../../../__mocks__/index_pattern'; +import { ElasticSearchHit } from '../../doc_views/doc_views_types'; jest.mock('../../../../../kibana_react/public', () => ({ useUiSetting: () => true, @@ -26,7 +27,7 @@ jest.mock('../../../kibana_services', () => ({ }), })); -const rowsSource = [ +const rowsSource: ElasticSearchHit[] = [ { _id: '1', _index: 'test', @@ -34,12 +35,12 @@ const rowsSource = [ _score: 1, _source: { bytes: 100, extension: '.gz' }, highlight: { - extension: '@kibana-highlighted-field.gz@/kibana-highlighted-field', + extension: ['@kibana-highlighted-field.gz@/kibana-highlighted-field'], }, }, ]; -const rowsFields = [ +const rowsFields: ElasticSearchHit[] = [ { _id: '1', _index: 'test', @@ -48,12 +49,12 @@ const rowsFields = [ _source: undefined, fields: { bytes: [100], extension: ['.gz'] }, highlight: { - extension: '@kibana-highlighted-field.gz@/kibana-highlighted-field', + extension: ['@kibana-highlighted-field.gz@/kibana-highlighted-field'], }, }, ]; -const rowsFieldsWithTopLevelObject = [ +const rowsFieldsWithTopLevelObject: ElasticSearchHit[] = [ { _id: '1', _index: 'test', @@ -62,7 +63,7 @@ const rowsFieldsWithTopLevelObject = [ _source: undefined, fields: { 'object.value': [100], extension: ['.gz'] }, highlight: { - extension: '@kibana-highlighted-field.gz@/kibana-highlighted-field', + extension: ['@kibana-highlighted-field.gz@/kibana-highlighted-field'], }, }, ]; @@ -167,7 +168,9 @@ describe('Discover grid cell rendering', function () { }, "_type": "test", "highlight": Object { - "extension": "@kibana-highlighted-field.gz@/kibana-highlighted-field", + "extension": Array [ + "@kibana-highlighted-field.gz@/kibana-highlighted-field", + ], }, } } @@ -264,7 +267,9 @@ describe('Discover grid cell rendering', function () { ], }, "highlight": Object { - "extension": "@kibana-highlighted-field.gz@/kibana-highlighted-field", + "extension": Array [ + "@kibana-highlighted-field.gz@/kibana-highlighted-field", + ], }, } } diff --git a/src/plugins/discover/public/application/components/table/table.test.tsx b/src/plugins/discover/public/application/components/table/table.test.tsx index 6de41aa0643a58..8997c1d13a4747 100644 --- a/src/plugins/discover/public/application/components/table/table.test.tsx +++ b/src/plugins/discover/public/application/components/table/table.test.tsx @@ -87,7 +87,7 @@ describe('DocViewTable at Discover', () => { scripted: 123, _underscore: 123, }, - }; + } as any; const props = { hit, @@ -185,7 +185,7 @@ describe('DocViewTable at Discover Context', () => { Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. \ Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestibulum volutpat pretium libero. Cras id dui. Aenean ut', }, - }; + } as any; const props = { hit, columns: ['extension'], diff --git a/src/plugins/discover/public/application/doc_views/doc_views_types.ts b/src/plugins/discover/public/application/doc_views/doc_views_types.ts index b06b242ee9ea3c..02ac951f7f57ca 100644 --- a/src/plugins/discover/public/application/doc_views/doc_views_types.ts +++ b/src/plugins/discover/public/application/doc_views/doc_views_types.ts @@ -8,7 +8,7 @@ import { ComponentType } from 'react'; import { IScope } from 'angular'; -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { IndexPattern } from '../../../../data/public'; export interface AngularDirective { @@ -18,7 +18,7 @@ export interface AngularDirective { export type AngularScope = IScope; -export type ElasticSearchHit = SearchResponse['hits']['hits'][number]; +export type ElasticSearchHit = estypes.SearchResponse['hits']['hits'][number]; export interface FieldMapping { filterable?: boolean; diff --git a/src/plugins/discover/public/application/embeddable/search_embeddable.ts b/src/plugins/discover/public/application/embeddable/search_embeddable.ts index 829d23fa071fb4..e7349ed22355ab 100644 --- a/src/plugins/discover/public/application/embeddable/search_embeddable.ts +++ b/src/plugins/discover/public/application/embeddable/search_embeddable.ts @@ -357,7 +357,7 @@ export class SearchEmbeddable // Apply the changes to the angular scope this.searchScope.$apply(() => { this.searchScope!.hits = resp.hits.hits; - this.searchScope!.totalHitCount = resp.hits.total; + this.searchScope!.totalHitCount = resp.hits.total as number; this.searchScope!.isLoading = false; }); } catch (error) { diff --git a/src/plugins/embeddable/public/public.api.md b/src/plugins/embeddable/public/public.api.md index 189f71b85206bc..b9719542adc813 100644 --- a/src/plugins/embeddable/public/public.api.md +++ b/src/plugins/embeddable/public/public.api.md @@ -15,6 +15,7 @@ import * as CSS from 'csstype'; import { DetailedPeerCertificate } from 'tls'; import { EmbeddableStart as EmbeddableStart_2 } from 'src/plugins/embeddable/public/plugin'; import { EnvironmentMode } from '@kbn/config'; +import { estypes } from '@elastic/elasticsearch'; import { EuiBreadcrumb } from '@elastic/eui'; import { EuiButtonEmptyProps } from '@elastic/eui'; import { EuiConfirmModalProps } from '@elastic/eui'; diff --git a/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/lib/get_indices.ts b/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/lib/get_indices.ts index b20d2bcca21da2..90865bf911934e 100644 --- a/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/lib/get_indices.ts +++ b/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/lib/get_indices.ts @@ -41,6 +41,7 @@ export const searchResponseToArray = ( if (!rawResponse.aggregations) { return []; } else { + // @ts-expect-error @elastic/elasticsearch no way to declare a type for aggregation in the search response return rawResponse.aggregations.indices.buckets .map((bucket: { key: string }) => { return bucket.key; diff --git a/src/plugins/index_pattern_management/server/routes/preview_scripted_field.test.ts b/src/plugins/index_pattern_management/server/routes/preview_scripted_field.test.ts index 385b4f04c40f15..1343b20365a442 100644 --- a/src/plugins/index_pattern_management/server/routes/preview_scripted_field.test.ts +++ b/src/plugins/index_pattern_management/server/routes/preview_scripted_field.test.ts @@ -46,8 +46,8 @@ describe('preview_scripted_field route', () => { expect(mockClient.search.mock.calls[0][0]).toMatchInlineSnapshot(` Object { - "_source": undefined, "body": Object { + "_source": undefined, "query": Object { "match_all": Object {}, }, @@ -59,10 +59,10 @@ describe('preview_scripted_field route', () => { }, }, }, + "size": 10, + "timeout": "30s", }, "index": "kibana_sample_data_logs", - "size": 10, - "timeout": "30s", } `); @@ -102,12 +102,12 @@ describe('preview_scripted_field route', () => { expect(mockClient.search.mock.calls[0][0]).toMatchInlineSnapshot(` Object { - "_source": Array [ - "a", - "b", - "c", - ], "body": Object { + "_source": Array [ + "a", + "b", + "c", + ], "query": Object { "bool": Object { "some": "query", @@ -121,10 +121,10 @@ describe('preview_scripted_field route', () => { }, }, }, + "size": 10, + "timeout": "30s", }, "index": "kibana_sample_data_logs", - "size": 10, - "timeout": "30s", } `); }); diff --git a/src/plugins/index_pattern_management/server/routes/preview_scripted_field.ts b/src/plugins/index_pattern_management/server/routes/preview_scripted_field.ts index 276f6dc0db8bf7..cc161859f4189e 100644 --- a/src/plugins/index_pattern_management/server/routes/preview_scripted_field.ts +++ b/src/plugins/index_pattern_management/server/routes/preview_scripted_field.ts @@ -30,10 +30,10 @@ export function registerPreviewScriptedFieldRoute(router: IRouter): void { try { const response = await client.search({ index, - _source: additionalFields && additionalFields.length > 0 ? additionalFields : undefined, - size: 10, - timeout: '30s', body: { + _source: additionalFields && additionalFields.length > 0 ? additionalFields : undefined, + size: 10, + timeout: '30s', query: query ?? { match_all: {} }, script_fields: { [name]: { diff --git a/src/plugins/kibana_usage_collection/server/collectors/kibana/get_saved_object_counts.ts b/src/plugins/kibana_usage_collection/server/collectors/kibana/get_saved_object_counts.ts index 52ba793882a1d8..42363f71ef87aa 100644 --- a/src/plugins/kibana_usage_collection/server/collectors/kibana/get_saved_object_counts.ts +++ b/src/plugins/kibana_usage_collection/server/collectors/kibana/get_saved_object_counts.ts @@ -58,6 +58,7 @@ export async function getSavedObjectsCounts( }; const { body } = await esClient.search(savedObjectCountSearchParams); const buckets: Array<{ key: string; doc_count: number }> = + // @ts-expect-error @elastic/elasticsearch Aggregate does not include `buckets` body.aggregations?.types?.buckets || []; // Initialise the object with all zeros for all the types diff --git a/src/plugins/security_oss/server/check_cluster_data.test.ts b/src/plugins/security_oss/server/check_cluster_data.test.ts index 0670eb3116b071..9e9459a68754cc 100644 --- a/src/plugins/security_oss/server/check_cluster_data.test.ts +++ b/src/plugins/security_oss/server/check_cluster_data.test.ts @@ -27,6 +27,7 @@ describe('checkClusterForUserData', () => { it('returns false if data only exists in system indices', async () => { const esClient = elasticsearchServiceMock.createElasticsearchClient(); esClient.cat.indices.mockResolvedValue( + // @ts-expect-error @elastic/elasticsearch ES types don't support array response format elasticsearchServiceMock.createApiResponse({ body: [ { @@ -55,6 +56,7 @@ describe('checkClusterForUserData', () => { it('returns true if data exists in non-system indices', async () => { const esClient = elasticsearchServiceMock.createElasticsearchClient(); esClient.cat.indices.mockResolvedValue( + // @ts-expect-error @elastic/elasticsearch ES types don't support array response format elasticsearchServiceMock.createApiResponse({ body: [ { @@ -85,6 +87,7 @@ describe('checkClusterForUserData', () => { ) .mockRejectedValueOnce(new Error('something terrible happened')) .mockResolvedValueOnce( + // @ts-expect-error @elastic/elasticsearch ES types don't support array response format elasticsearchServiceMock.createApiResponse({ body: [ { @@ -95,6 +98,7 @@ describe('checkClusterForUserData', () => { }) ) .mockResolvedValueOnce( + // @ts-expect-error @elastic/elasticsearch ES types don't support array response format elasticsearchServiceMock.createApiResponse({ body: [ { diff --git a/src/plugins/security_oss/server/check_cluster_data.ts b/src/plugins/security_oss/server/check_cluster_data.ts index c8c30196b485c0..19a4145333dd0c 100644 --- a/src/plugins/security_oss/server/check_cluster_data.ts +++ b/src/plugins/security_oss/server/check_cluster_data.ts @@ -14,17 +14,15 @@ export const createClusterDataCheck = () => { return async function doesClusterHaveUserData(esClient: ElasticsearchClient, log: Logger) { if (!clusterHasUserData) { try { - const indices = await esClient.cat.indices< - Array<{ index: string; ['docs.count']: string }> - >({ + const indices = await esClient.cat.indices({ format: 'json', h: ['index', 'docs.count'], }); clusterHasUserData = indices.body.some((indexCount) => { const isInternalIndex = - indexCount.index.startsWith('.') || indexCount.index.startsWith('kibana_sample_'); + indexCount.index?.startsWith('.') || indexCount.index?.startsWith('kibana_sample_'); - return !isInternalIndex && parseInt(indexCount['docs.count'], 10) > 0; + return !isInternalIndex && parseInt(indexCount['docs.count']!, 10) > 0; }); } catch (e) { log.warn(`Error encountered while checking cluster for user data: ${e}`); diff --git a/src/plugins/telemetry/server/telemetry_collection/get_cluster_info.ts b/src/plugins/telemetry/server/telemetry_collection/get_cluster_info.ts index 437d76fe7ccf2b..3f93bde1e7e625 100644 --- a/src/plugins/telemetry/server/telemetry_collection/get_cluster_info.ts +++ b/src/plugins/telemetry/server/telemetry_collection/get_cluster_info.ts @@ -8,22 +8,6 @@ import { ElasticsearchClient } from 'src/core/server'; -// This can be removed when the ES client improves the types -export interface ESClusterInfo { - cluster_uuid: string; - cluster_name: string; - version: { - number: string; - build_flavor?: string; - build_type?: string; - build_hash?: string; - build_date?: string; - build_snapshot?: boolean; - lucene_version?: string; - minimum_wire_compatibility_version?: string; - minimum_index_compatibility_version?: string; - }; -} /** * Get the cluster info from the connected cluster. * @@ -32,6 +16,6 @@ export interface ESClusterInfo { * @param {function} esClient The asInternalUser handler (exposed for testing) */ export async function getClusterInfo(esClient: ElasticsearchClient) { - const { body } = await esClient.info(); + const { body } = await esClient.info(); return body; } diff --git a/src/plugins/telemetry/server/telemetry_collection/get_data_telemetry/get_data_telemetry.ts b/src/plugins/telemetry/server/telemetry_collection/get_data_telemetry/get_data_telemetry.ts index 42ccbcc46c4627..c79c46072e11be 100644 --- a/src/plugins/telemetry/server/telemetry_collection/get_data_telemetry/get_data_telemetry.ts +++ b/src/plugins/telemetry/server/telemetry_collection/get_data_telemetry/get_data_telemetry.ts @@ -261,14 +261,16 @@ export async function getDataTelemetry(esClient: ElasticsearchClient) { const indices = indexNames.map((name) => { const baseIndexInfo = { name, - isECS: !!indexMappings[name]?.mappings?.properties.ecs?.properties.version?.type, + isECS: !!indexMappings[name]?.mappings?.properties?.ecs?.properties?.version?.type, shipper: indexMappings[name]?.mappings?._meta?.beat, packageName: indexMappings[name]?.mappings?._meta?.package?.name, managedBy: indexMappings[name]?.mappings?._meta?.managed_by, dataStreamDataset: - indexMappings[name]?.mappings?.properties.data_stream?.properties.dataset?.value, + // @ts-expect-error @elastic/elasticsearch PropertyBase doesn't decalre value + indexMappings[name]?.mappings?.properties?.data_stream?.properties?.dataset?.value, dataStreamType: - indexMappings[name]?.mappings?.properties.data_stream?.properties.type?.value, + // @ts-expect-error @elastic/elasticsearch PropertyBase doesn't decalre value + indexMappings[name]?.mappings?.properties?.data_stream?.properties?.type?.value, }; const stats = (indexStats?.indices || {})[name]; diff --git a/src/plugins/telemetry/server/telemetry_collection/get_local_stats.test.ts b/src/plugins/telemetry/server/telemetry_collection/get_local_stats.test.ts index 47c6736ff9aea4..edf8dbb30809bf 100644 --- a/src/plugins/telemetry/server/telemetry_collection/get_local_stats.test.ts +++ b/src/plugins/telemetry/server/telemetry_collection/get_local_stats.test.ts @@ -7,6 +7,7 @@ */ import { merge, omit } from 'lodash'; +import type { estypes } from '@elastic/elasticsearch'; import { getLocalStats, handleLocalStats } from './get_local_stats'; import { @@ -34,35 +35,33 @@ function mockGetLocalStats(clusterInfo: any, clusterStats: any) { esClient.cluster.stats // @ts-expect-error we only care about the response body .mockResolvedValue({ body: { ...clusterStats } }); - esClient.nodes.usage.mockResolvedValue( + esClient.nodes.usage.mockResolvedValue({ // @ts-expect-error we only care about the response body - { - body: { - cluster_name: 'testCluster', - nodes: { - some_node_id: { - timestamp: 1588617023177, - since: 1588616945163, - rest_actions: { - nodes_usage_action: 1, - create_index_action: 1, - document_get_action: 1, - search_action: 19, - nodes_info_action: 36, + body: { + cluster_name: 'testCluster', + nodes: { + some_node_id: { + timestamp: 1588617023177, + since: 1588616945163, + rest_actions: { + nodes_usage_action: 1, + create_index_action: 1, + document_get_action: 1, + search_action: 19, + nodes_info_action: 36, + }, + aggregations: { + scripted_metric: { + other: 7, }, - aggregations: { - terms: { - bytes: 2, - }, - scripted_metric: { - other: 7, - }, + terms: { + bytes: 2, }, }, }, }, - } - ); + }, + }); // @ts-expect-error we only care about the response body esClient.indices.getMapping.mockResolvedValue({ body: { mappings: {} } }); // @ts-expect-error we only care about the response body @@ -188,7 +187,7 @@ describe('get_local_stats', () => { describe('handleLocalStats', () => { it('returns expected object without xpack or kibana data', () => { const result = handleLocalStats( - clusterInfo, + clusterInfo as estypes.RootNodeInfoResponse, clusterStatsWithNodesUsage, void 0, void 0, @@ -205,7 +204,7 @@ describe('get_local_stats', () => { it('returns expected object with xpack', () => { const result = handleLocalStats( - clusterInfo, + clusterInfo as estypes.RootNodeInfoResponse, clusterStatsWithNodesUsage, void 0, void 0, diff --git a/src/plugins/telemetry/server/telemetry_collection/get_local_stats.ts b/src/plugins/telemetry/server/telemetry_collection/get_local_stats.ts index 710d836576d105..67f9ebb8ff3e45 100644 --- a/src/plugins/telemetry/server/telemetry_collection/get_local_stats.ts +++ b/src/plugins/telemetry/server/telemetry_collection/get_local_stats.ts @@ -6,11 +6,12 @@ * Side Public License, v 1. */ +import type { estypes } from '@elastic/elasticsearch'; import { StatsGetter, StatsCollectionContext, } from 'src/plugins/telemetry_collection_manager/server'; -import { getClusterInfo, ESClusterInfo } from './get_cluster_info'; +import { getClusterInfo } from './get_cluster_info'; import { getClusterStats } from './get_cluster_stats'; import { getKibana, handleKibanaStats, KibanaUsageStats } from './get_kibana'; import { getNodesUsage } from './get_nodes_usage'; @@ -27,7 +28,7 @@ import { getDataTelemetry, DATA_TELEMETRY_ID, DataTelemetryPayload } from './get */ export function handleLocalStats( // eslint-disable-next-line @typescript-eslint/naming-convention - { cluster_name, cluster_uuid, version }: ESClusterInfo, + { cluster_name, cluster_uuid, version }: estypes.RootNodeInfoResponse, { _nodes, cluster_name: clusterName, ...clusterStats }: any, kibana: KibanaUsageStats | undefined, dataTelemetry: DataTelemetryPayload | undefined, diff --git a/src/plugins/telemetry/server/telemetry_collection/get_nodes_usage.ts b/src/plugins/telemetry/server/telemetry_collection/get_nodes_usage.ts index 18c6d16447238e..e46d4be5407349 100644 --- a/src/plugins/telemetry/server/telemetry_collection/get_nodes_usage.ts +++ b/src/plugins/telemetry/server/telemetry_collection/get_nodes_usage.ts @@ -16,7 +16,7 @@ export interface NodeAggregation { // we set aggregations as an optional type because it was only added in v7.8.0 export interface NodeObj { node_id?: string; - timestamp: number; + timestamp: number | string; since: number; rest_actions: { [key: string]: number; @@ -46,9 +46,10 @@ export type NodesUsageGetter = ( export async function fetchNodesUsage( esClient: ElasticsearchClient ): Promise { - const { body } = await esClient.nodes.usage({ + const { body } = await esClient.nodes.usage({ timeout: TIMEOUT, }); + // @ts-expect-error TODO: Does the client parse `timestamp` to a Date object? Expected a number return body; } diff --git a/src/plugins/vis_type_vega/public/data_model/types.ts b/src/plugins/vis_type_vega/public/data_model/types.ts index 042ffac583e987..8590b51d3b5ffe 100644 --- a/src/plugins/vis_type_vega/public/data_model/types.ts +++ b/src/plugins/vis_type_vega/public/data_model/types.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { SearchResponse, SearchParams } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { Filter } from 'src/plugins/data/public'; import { DslQuery } from 'src/plugins/data/common'; @@ -17,7 +17,7 @@ import { EmsFileParser } from './ems_file_parser'; import { UrlParser } from './url_parser'; interface Body { - aggs?: SearchParams['body']['aggs']; + aggs?: Record; query?: Query; timeout?: string; } @@ -76,7 +76,7 @@ interface Projection { interface RequestDataObject { name?: string; url?: TUrlData; - values: SearchResponse; + values: estypes.SearchResponse; } type ContextVarsObjectProps = diff --git a/src/plugins/vis_type_vega/server/usage_collector/get_usage_collector.ts b/src/plugins/vis_type_vega/server/usage_collector/get_usage_collector.ts index 8d3512aa2138e5..d5f8d978d52521 100644 --- a/src/plugins/vis_type_vega/server/usage_collector/get_usage_collector.ts +++ b/src/plugins/vis_type_vega/server/usage_collector/get_usage_collector.ts @@ -7,14 +7,12 @@ */ import { parse } from 'hjson'; -import { SearchResponse } from 'elasticsearch'; import { ElasticsearchClient, SavedObject } from 'src/core/server'; import { VegaSavedObjectAttributes, VisTypeVegaPluginSetupDependencies } from '../types'; type UsageCollectorDependencies = Pick; -type ESResponse = SearchResponse<{ visualization: { visState: string } }>; type VegaType = 'vega' | 'vega-lite'; function isVegaType(attributes: any): attributes is VegaSavedObjectAttributes { @@ -80,7 +78,9 @@ export const getStats = async ( }, }; - const { body: esResponse } = await esClient.search(searchParams); + const { body: esResponse } = await esClient.search<{ visualization: { visState: string } }>( + searchParams + ); const size = esResponse?.hits?.hits?.length ?? 0; if (!size) { diff --git a/src/plugins/visualizations/server/usage_collector/get_usage_collector.ts b/src/plugins/visualizations/server/usage_collector/get_usage_collector.ts index 164d5b5fa72ac6..89e1e7f03e1495 100644 --- a/src/plugins/visualizations/server/usage_collector/get_usage_collector.ts +++ b/src/plugins/visualizations/server/usage_collector/get_usage_collector.ts @@ -61,7 +61,7 @@ export async function getStats( // `map` to get the raw types const visSummaries: VisSummary[] = esResponse.hits.hits.map((hit) => { - const spacePhrases = hit._id.split(':'); + const spacePhrases = hit._id.toString().split(':'); const lastUpdated: string = get(hit, '_source.updated_at'); const space = spacePhrases.length === 3 ? spacePhrases[0] : 'default'; // if in a custom space, the format of a saved object ID is space:type:id const visualization = get(hit, '_source.visualization', { visState: '{}' }); diff --git a/test/api_integration/apis/home/sample_data.ts b/test/api_integration/apis/home/sample_data.ts index b889b59fdaf329..99327901ec8c35 100644 --- a/test/api_integration/apis/home/sample_data.ts +++ b/test/api_integration/apis/home/sample_data.ts @@ -48,12 +48,12 @@ export default function ({ getService }: FtrProviderContext) { }); it('should load elasticsearch index containing sample data with dates relative to current time', async () => { - const { body: resp } = await es.search({ + const { body: resp } = await es.search<{ timestamp: string }>({ index: 'kibana_sample_data_flights', }); const doc = resp.hits.hits[0]; - const docMilliseconds = Date.parse(doc._source.timestamp); + const docMilliseconds = Date.parse(doc._source!.timestamp); const nowMilliseconds = Date.now(); const delta = Math.abs(nowMilliseconds - docMilliseconds); expect(delta).to.be.lessThan(MILLISECOND_IN_WEEK * 4); @@ -66,12 +66,12 @@ export default function ({ getService }: FtrProviderContext) { .post(`/api/sample_data/flights?now=${nowString}`) .set('kbn-xsrf', 'kibana'); - const { body: resp } = await es.search({ + const { body: resp } = await es.search<{ timestamp: string }>({ index: 'kibana_sample_data_flights', }); const doc = resp.hits.hits[0]; - const docMilliseconds = Date.parse(doc._source.timestamp); + const docMilliseconds = Date.parse(doc._source!.timestamp); const nowMilliseconds = Date.parse(nowString); const delta = Math.abs(nowMilliseconds - docMilliseconds); expect(delta).to.be.lessThan(MILLISECOND_IN_WEEK * 4); diff --git a/test/api_integration/apis/saved_objects/migrations.ts b/test/api_integration/apis/saved_objects/migrations.ts index 1f1f1a5c98cd64..87997ab4231a26 100644 --- a/test/api_integration/apis/saved_objects/migrations.ts +++ b/test/api_integration/apis/saved_objects/migrations.ts @@ -15,7 +15,7 @@ import { set } from '@elastic/safer-lodash-set'; import _ from 'lodash'; import expect from '@kbn/expect'; import { ElasticsearchClient, SavedObjectsType } from 'src/core/server'; -import { SearchResponse } from '../../../../src/core/server/elasticsearch/client'; + import { DocumentMigrator, IndexMigrator, @@ -113,7 +113,7 @@ export default ({ getService }: FtrProviderContext) => { await esClient.indices.putTemplate({ name: 'migration_test_a_template', body: { - index_patterns: 'migration_test_a', + index_patterns: ['migration_test_a'], mappings: { dynamic: 'strict', properties: { baz: { type: 'text' } }, @@ -125,7 +125,7 @@ export default ({ getService }: FtrProviderContext) => { await esClient.indices.putTemplate({ name: 'migration_a_template', body: { - index_patterns: index, + index_patterns: [index], mappings: { dynamic: 'strict', properties: { baz: { type: 'text' } }, @@ -744,7 +744,7 @@ async function migrateIndex({ } async function fetchDocs(esClient: ElasticsearchClient, index: string) { - const { body } = await esClient.search>({ index }); + const { body } = await esClient.search({ index }); return body.hits.hits .map((h) => ({ diff --git a/test/api_integration/apis/telemetry/telemetry_optin_notice_seen.ts b/test/api_integration/apis/telemetry/telemetry_optin_notice_seen.ts index 2c58794c96eca0..a76d09481eca1a 100644 --- a/test/api_integration/apis/telemetry/telemetry_optin_notice_seen.ts +++ b/test/api_integration/apis/telemetry/telemetry_optin_notice_seen.ts @@ -26,15 +26,13 @@ export default function optInTest({ getService }: FtrProviderContext) { await supertest.put('/api/telemetry/v2/userHasSeenNotice').set('kbn-xsrf', 'xxx').expect(200); const { - body: { - _source: { telemetry }, - }, - } = await client.get({ + body: { _source }, + } = await client.get<{ telemetry: { userHasSeenNotice: boolean } }>({ index: '.kibana', id: 'telemetry:telemetry', }); - expect(telemetry.userHasSeenNotice).to.be(true); + expect(_source?.telemetry.userHasSeenNotice).to.be(true); }); }); } diff --git a/test/api_integration/apis/ui_metric/ui_metric.ts b/test/api_integration/apis/ui_metric/ui_metric.ts index 99007376e1ea49..47d10da9a1b29c 100644 --- a/test/api_integration/apis/ui_metric/ui_metric.ts +++ b/test/api_integration/apis/ui_metric/ui_metric.ts @@ -102,12 +102,12 @@ export default function ({ getService }: FtrProviderContext) { body: { hits: { hits }, }, - } = await es.search({ index: '.kibana', q: 'type:ui-metric' }); + } = await es.search({ index: '.kibana', q: 'type:ui-metric' }); const countTypeEvent = hits.find( (hit: { _id: string }) => hit._id === `ui-metric:myApp:${uniqueEventName}` ); - expect(countTypeEvent._source['ui-metric'].count).to.eql(3); + expect(countTypeEvent?._source['ui-metric'].count).to.eql(3); }); }); } diff --git a/test/common/services/elasticsearch.ts b/test/common/services/elasticsearch.ts index 99335f84058284..7b8ff6bd6c8f4c 100644 --- a/test/common/services/elasticsearch.ts +++ b/test/common/services/elasticsearch.ts @@ -10,10 +10,14 @@ import { format as formatUrl } from 'url'; import fs from 'fs'; import { Client } from '@elastic/elasticsearch'; import { CA_CERT_PATH } from '@kbn/dev-utils'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { FtrProviderContext } from '../ftr_provider_context'; -export function ElasticsearchProvider({ getService }: FtrProviderContext) { +/* + registers Kibana-specific @elastic/elasticsearch client instance. + */ +export function ElasticsearchProvider({ getService }: FtrProviderContext): KibanaClient { const config = getService('config'); if (process.env.TEST_CLOUD) { diff --git a/typings/elasticsearch/aggregations.d.ts b/typings/elasticsearch/aggregations.d.ts deleted file mode 100644 index 2b501c94889f48..00000000000000 --- a/typings/elasticsearch/aggregations.d.ts +++ /dev/null @@ -1,466 +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 { Unionize, UnionToIntersection } from 'utility-types'; -import { ESSearchHit, MaybeReadonlyArray, ESSourceOptions, ESHitsOf } from '.'; - -export type SortOrder = 'asc' | 'desc'; -type SortInstruction = Record; -export type SortOptions = SortOrder | SortInstruction | SortInstruction[]; - -type Script = - | string - | { - lang?: string; - id?: string; - source?: string; - params?: Record; - }; - -type BucketsPath = string | Record; - -type AggregationSourceOptions = - | { - field: string; - missing?: unknown; - } - | { - script: Script; - }; - -interface MetricsAggregationResponsePart { - value: number | null; -} -interface DateHistogramBucket { - doc_count: number; - key: number; - key_as_string: string; -} - -type GetCompositeKeys< - TAggregationOptionsMap extends AggregationOptionsMap -> = TAggregationOptionsMap extends { - composite: { sources: Array }; -} - ? keyof Source - : never; - -type CompositeOptionsSource = Record< - string, - | { - terms: ({ field: string } | { script: Script }) & { - missing_bucket?: boolean; - }; - } - | undefined ->; - -export interface AggregationOptionsByType { - terms: { - size?: number; - order?: SortOptions; - execution_hint?: 'map' | 'global_ordinals'; - } & AggregationSourceOptions; - date_histogram: { - format?: string; - min_doc_count?: number; - extended_bounds?: { - min: number; - max: number; - }; - } & ({ calendar_interval: string } | { fixed_interval: string }) & - AggregationSourceOptions; - histogram: { - interval: number; - min_doc_count?: number; - extended_bounds?: { - min?: number | string; - max?: number | string; - }; - } & AggregationSourceOptions; - avg: AggregationSourceOptions; - max: AggregationSourceOptions; - min: AggregationSourceOptions; - sum: AggregationSourceOptions; - value_count: AggregationSourceOptions; - cardinality: AggregationSourceOptions & { - precision_threshold?: number; - }; - percentiles: { - percents?: number[]; - hdr?: { number_of_significant_value_digits: number }; - } & AggregationSourceOptions; - stats: { - field: string; - }; - extended_stats: { - field: string; - }; - string_stats: { field: string }; - top_hits: { - from?: number; - size?: number; - sort?: SortOptions; - _source?: ESSourceOptions; - fields?: MaybeReadonlyArray; - docvalue_fields?: MaybeReadonlyArray; - }; - filter: Record; - filters: { - filters: Record | any[]; - }; - sampler: { - shard_size?: number; - }; - derivative: { - buckets_path: BucketsPath; - }; - bucket_script: { - buckets_path: BucketsPath; - script?: Script; - }; - composite: { - size?: number; - sources: CompositeOptionsSource[]; - after?: Record; - }; - diversified_sampler: { - shard_size?: number; - max_docs_per_value?: number; - } & ({ script: Script } | { field: string }); // TODO use MetricsAggregationOptions if possible - scripted_metric: { - params?: Record; - init_script?: Script; - map_script: Script; - combine_script: Script; - reduce_script: Script; - }; - date_range: { - format?: string; - ranges: Array< - | { from: string | number } - | { to: string | number } - | { from: string | number; to: string | number } - >; - keyed?: boolean; - } & AggregationSourceOptions; - range: { - field: string; - ranges: Array< - | { key?: string; from: string | number } - | { key?: string; to: string | number } - | { key?: string; from: string | number; to: string | number } - >; - keyed?: boolean; - }; - auto_date_histogram: { - buckets: number; - } & AggregationSourceOptions; - percentile_ranks: { - values: Array; - keyed?: boolean; - hdr?: { number_of_significant_value_digits: number }; - } & AggregationSourceOptions; - bucket_sort: { - sort?: SortOptions; - from?: number; - size?: number; - }; - significant_terms: { - size?: number; - field?: string; - background_filter?: Record; - } & AggregationSourceOptions; - bucket_selector: { - buckets_path: { - [x: string]: string; - }; - script: string; - }; - top_metrics: { - metrics: { field: string } | MaybeReadonlyArray<{ field: string }>; - sort: SortOptions; - }; - avg_bucket: { - buckets_path: string; - gap_policy?: 'skip' | 'insert_zeros'; - format?: string; - }; - rate: { - unit: 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year'; - } & ( - | { - field: string; - mode: 'sum' | 'value_count'; - } - | {} - ); -} - -type AggregationType = keyof AggregationOptionsByType; - -type AggregationOptionsMap = Unionize< - { - [TAggregationType in AggregationType]: AggregationOptionsByType[TAggregationType]; - } -> & { aggs?: AggregationInputMap }; - -interface DateRangeBucket { - key: string; - to?: number; - from?: number; - to_as_string?: string; - from_as_string?: string; - doc_count: number; -} - -export interface AggregationInputMap { - [key: string]: AggregationOptionsMap; -} - -type SubAggregationResponseOf< - TAggregationInputMap extends AggregationInputMap | undefined, - TDocument -> = TAggregationInputMap extends AggregationInputMap - ? AggregationResponseMap - : {}; - -interface AggregationResponsePart { - terms: { - buckets: Array< - { - doc_count: number; - key: string | number; - } & SubAggregationResponseOf - >; - doc_count_error_upper_bound?: number; - sum_other_doc_count?: number; - }; - histogram: { - buckets: Array< - { - doc_count: number; - key: number; - } & SubAggregationResponseOf - >; - }; - date_histogram: { - buckets: Array< - DateHistogramBucket & SubAggregationResponseOf - >; - }; - avg: MetricsAggregationResponsePart; - sum: MetricsAggregationResponsePart; - max: MetricsAggregationResponsePart; - min: MetricsAggregationResponsePart; - value_count: { value: number }; - cardinality: { - value: number; - }; - percentiles: { - values: Record; - }; - stats: { - count: number; - min: number | null; - max: number | null; - avg: number | null; - sum: number | null; - }; - extended_stats: { - count: number; - min: number | null; - max: number | null; - avg: number | null; - sum: number | null; - sum_of_squares: number | null; - variance: number | null; - std_deviation: number | null; - std_deviation_bounds: { - upper: number | null; - lower: number | null; - }; - }; - string_stats: { - count: number; - min_length: number; - max_length: number; - avg_length: number; - entropy: number; - }; - top_hits: { - hits: { - total: { - value: number; - relation: 'eq' | 'gte'; - }; - max_score: number | null; - hits: TAggregationOptionsMap extends { top_hits: AggregationOptionsByType['top_hits'] } - ? ESHitsOf - : ESSearchHit[]; - }; - }; - filter: { - doc_count: number; - } & SubAggregationResponseOf; - filters: TAggregationOptionsMap extends { filters: { filters: any[] } } - ? Array< - { doc_count: number } & AggregationResponseMap - > - : TAggregationOptionsMap extends { - filters: { - filters: Record; - }; - } - ? { - buckets: { - [key in keyof TAggregationOptionsMap['filters']['filters']]: { - doc_count: number; - } & SubAggregationResponseOf; - }; - } - : never; - sampler: { - doc_count: number; - } & SubAggregationResponseOf; - derivative: - | { - value: number; - } - | undefined; - bucket_script: - | { - value: number | null; - } - | undefined; - composite: { - after_key: { - [key in GetCompositeKeys]: TAggregationOptionsMap; - }; - buckets: Array< - { - key: Record, string | number>; - doc_count: number; - } & SubAggregationResponseOf - >; - }; - diversified_sampler: { - doc_count: number; - } & AggregationResponseMap; - scripted_metric: { - value: unknown; - }; - date_range: { - buckets: TAggregationOptionsMap extends { date_range: { keyed: true } } - ? Record - : { buckets: DateRangeBucket[] }; - }; - range: { - buckets: TAggregationOptionsMap extends { range: { keyed: true } } - ? Record< - string, - DateRangeBucket & SubAggregationResponseOf - > - : Array< - DateRangeBucket & SubAggregationResponseOf - >; - }; - auto_date_histogram: { - buckets: Array< - DateHistogramBucket & AggregationResponseMap - >; - interval: string; - }; - - percentile_ranks: { - values: TAggregationOptionsMap extends { - percentile_ranks: { keyed: false }; - } - ? Array<{ key: number; value: number }> - : Record; - }; - significant_terms: { - doc_count: number; - bg_count: number; - buckets: Array< - { - score: number; - bg_count: number; - doc_count: number; - key: string | number; - } & SubAggregationResponseOf - >; - }; - bucket_sort: undefined; - bucket_selector: undefined; - top_metrics: { - top: [ - { - sort: [string | number]; - metrics: UnionToIntersection< - TAggregationOptionsMap extends { - top_metrics: { metrics: { field: infer TFieldName } }; - } - ? TopMetricsMap - : TAggregationOptionsMap extends { - top_metrics: { metrics: MaybeReadonlyArray<{ field: infer TFieldName }> }; - } - ? TopMetricsMap - : TopMetricsMap - >; - } - ]; - }; - avg_bucket: { - value: number | null; - }; - rate: { - value: number | null; - }; -} - -type TopMetricsMap = TFieldName extends string - ? Record - : Record; - -// Type for debugging purposes. If you see an error in AggregationResponseMap -// similar to "cannot be used to index type", uncomment the type below and hover -// over it to see what aggregation response types are missing compared to the -// input map. - -// type MissingAggregationResponseTypes = Exclude< -// AggregationType, -// keyof AggregationResponsePart<{}, unknown> -// >; - -// ensures aggregations work with requests where aggregation options are a union type, -// e.g. { transaction_groups: { composite: any } | { terms: any } }. -// Union keys are not included in keyof. The type will fall back to keyof T if -// UnionToIntersection fails, which happens when there are conflicts between the union -// types, e.g. { foo: string; bar?: undefined } | { foo?: undefined; bar: string }; -export type ValidAggregationKeysOf< - T extends Record -> = keyof (UnionToIntersection extends never ? T : UnionToIntersection); - -export type AggregationResultOf< - TAggregationOptionsMap extends AggregationOptionsMap, - TDocument -> = AggregationResponsePart[AggregationType & - ValidAggregationKeysOf]; - -export type AggregationResponseMap< - TAggregationInputMap extends AggregationInputMap | undefined, - TDocument -> = TAggregationInputMap extends AggregationInputMap - ? { - [TName in keyof TAggregationInputMap]: AggregationResponsePart< - TAggregationInputMap[TName], - TDocument - >[AggregationType & ValidAggregationKeysOf]; - } - : undefined; diff --git a/typings/elasticsearch/index.d.ts b/typings/elasticsearch/index.d.ts index a84d4148f6fe77..7eaf762d353ac4 100644 --- a/typings/elasticsearch/index.d.ts +++ b/typings/elasticsearch/index.d.ts @@ -5,136 +5,28 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ +import { estypes } from '@elastic/elasticsearch'; +import { InferSearchResponseOf, AggregateOf as AggregationResultOf, SearchHit } from './search'; -import { ValuesType } from 'utility-types'; -import { Explanation, SearchParams, SearchResponse } from 'elasticsearch'; -import { RequestParams } from '@elastic/elasticsearch'; -import { AggregationResponseMap, AggregationInputMap, SortOptions } from './aggregations'; -export { - AggregationInputMap, - AggregationOptionsByType, - AggregationResponseMap, - AggregationResultOf, - SortOptions, - ValidAggregationKeysOf, -} from './aggregations'; +export type ESFilter = estypes.QueryContainer; +export type ESSearchRequest = estypes.SearchRequest; +export type AggregationOptionsByType = Required; // Typings for Elasticsearch queries and aggregations. These are intended to be // moved to the Elasticsearch JS client at some point (see #77720.) export type MaybeReadonlyArray = T[] | readonly T[]; -interface CollapseQuery { - field: string; - inner_hits?: { - name: string; - size?: number; - sort?: SortOptions; - _source?: - | string - | string[] - | { - includes?: string | string[]; - excludes?: string | string[]; - }; - collapse?: { - field: string; - }; - }; - max_concurrent_group_searches?: number; -} - export type ESSourceOptions = boolean | string | string[]; -export type ESHitsOf< - TOptions extends - | { - size?: number; - _source?: ESSourceOptions; - docvalue_fields?: MaybeReadonlyArray; - fields?: MaybeReadonlyArray; - } - | undefined, - TDocument extends unknown -> = Array< - ESSearchHit< - TOptions extends { _source: false } ? undefined : TDocument, - TOptions extends { fields: MaybeReadonlyArray } ? TOptions['fields'] : undefined, - TOptions extends { docvalue_fields: MaybeReadonlyArray } - ? TOptions['docvalue_fields'] - : undefined - > ->; - -export interface ESSearchBody { - query?: any; - size?: number; - from?: number; - aggs?: AggregationInputMap; - track_total_hits?: boolean | number; - collapse?: CollapseQuery; - search_after?: Array; - _source?: ESSourceOptions; -} - -export type ESSearchRequest = RequestParams.Search; - export interface ESSearchOptions { restTotalHitsAsInt: boolean; } -export type ESSearchHit< - TSource extends any = unknown, - TFields extends MaybeReadonlyArray | undefined = undefined, - TDocValueFields extends MaybeReadonlyArray | undefined = undefined -> = { - _index: string; - _type: string; - _id: string; - _score: number; - _version?: number; - _explanation?: Explanation; - highlight?: any; - inner_hits?: any; - matched_queries?: string[]; - sort?: string[]; -} & (TSource extends false ? {} : { _source: TSource }) & - (TFields extends MaybeReadonlyArray - ? { - fields: Partial, unknown[]>>; - } - : {}) & - (TDocValueFields extends MaybeReadonlyArray - ? { - fields: Partial, unknown[]>>; - } - : {}); - export type ESSearchResponse< - TDocument, - TSearchRequest extends ESSearchRequest, - TOptions extends ESSearchOptions = { restTotalHitsAsInt: false } -> = Omit, 'aggregations' | 'hits'> & - (TSearchRequest extends { body: { aggs: AggregationInputMap } } - ? { - aggregations?: AggregationResponseMap; - } - : {}) & { - hits: Omit['hits'], 'total' | 'hits'> & - (TOptions['restTotalHitsAsInt'] extends true - ? { - total: number; - } - : { - total: { - value: number; - relation: 'eq' | 'gte'; - }; - }) & { hits: ESHitsOf }; - }; + TDocument = unknown, + TSearchRequest extends ESSearchRequest = ESSearchRequest, + TOptions extends { restTotalHitsAsInt: boolean } = { restTotalHitsAsInt: false } +> = InferSearchResponseOf; -export interface ESFilter { - [key: string]: { - [key: string]: string | string[] | number | boolean | Record | ESFilter[]; - }; -} +export { InferSearchResponseOf, AggregationResultOf, SearchHit }; diff --git a/typings/elasticsearch/search.d.ts b/typings/elasticsearch/search.d.ts new file mode 100644 index 00000000000000..fce08df1c0a04f --- /dev/null +++ b/typings/elasticsearch/search.d.ts @@ -0,0 +1,577 @@ +/* + * 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 { ValuesType } from 'utility-types'; +import { estypes } from '@elastic/elasticsearch'; + +type InvalidAggregationRequest = unknown; + +// ensures aggregations work with requests where aggregation options are a union type, +// e.g. { transaction_groups: { composite: any } | { terms: any } }. +// Union keys are not included in keyof, but extends iterates over the types in a union. +type ValidAggregationKeysOf> = T extends T ? keyof T : never; + +type KeyOfSource = Record< + keyof T, + (T extends Record ? null : never) | string | number +>; + +type KeysOfSources = T extends [infer U, ...infer V] + ? KeyOfSource & KeysOfSources + : T extends Array + ? KeyOfSource + : {}; + +type CompositeKeysOf< + TAggregationContainer extends estypes.AggregationContainer +> = TAggregationContainer extends { + composite: { sources: [...infer TSource] }; +} + ? KeysOfSources + : unknown; + +type Source = estypes.SourceFilter | boolean | estypes.Fields; + +type ValueTypeOfField = T extends Record + ? ValuesType + : T extends string[] | number[] + ? ValueTypeOfField> + : T extends { field: estypes.Field } + ? T['field'] + : T extends string | number + ? T + : never; + +type MaybeArray = T | T[]; + +type Fields = MaybeArray; +type DocValueFields = MaybeArray; + +export type SearchHit< + TSource extends any = unknown, + TFields extends Fields | undefined = undefined, + TDocValueFields extends DocValueFields | undefined = undefined +> = Omit & + (TSource extends false ? {} : { _source: TSource }) & + (TFields extends estypes.Fields + ? { + fields: Partial, unknown[]>>; + } + : {}) & + (TDocValueFields extends DocValueFields + ? { + fields: Partial, unknown[]>>; + } + : {}); + +type HitsOf< + TOptions extends + | { _source?: Source; fields?: Fields; docvalue_fields?: DocValueFields } + | undefined, + TDocument extends unknown +> = Array< + SearchHit< + TOptions extends { _source: false } ? undefined : TDocument, + TOptions extends { fields: estypes.Fields } ? TOptions['fields'] : undefined, + TOptions extends { docvalue_fields: DocValueFields } ? TOptions['docvalue_fields'] : undefined + > +>; + +type AggregationTypeName = Exclude; + +type AggregationMap = Partial>; + +type TopLevelAggregationRequest = Pick; + +type MaybeKeyed< + TAggregationContainer, + TBucket, + TKeys extends string = string +> = TAggregationContainer extends Record + ? Record + : { buckets: TBucket[] }; + +export type AggregateOf< + TAggregationContainer extends estypes.AggregationContainer, + 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< + { + 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; + } & 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 } + : 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< + { + doc_count: number; + key: string; + } & SubAggregateOf + >; + }; + geotile_grid: { + buckets: Array< + { + 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: + | { + 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; + 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; + to?: number; + 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; + 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< + { + 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.TopHitsAggregation } + ? HitsOf + : estypes.HitsMetadata; + }; + }; + top_metrics: { + top: Array<{ + sort: number[] | string[]; + metrics: Record< + TAggregationContainer extends Record }> + ? TKeys + : string, + string | number | null + >; + }>; + }; + weighted_avg: { value: number | null }; + value_count: { + value: number; + }; + // t_test: {} not defined +})[ValidAggregationKeysOf & AggregationTypeName]; + +type AggregateOfMap = { + [TAggregationName in keyof TAggregationMap]: TAggregationMap[TAggregationName] extends estypes.AggregationContainer + ? AggregateOf + : never; // using never means we effectively ignore optional keys, using {} creates a union type of { ... } | {} +}; + +type SubAggregateOf = TAggregationRequest extends { + aggs?: AggregationMap; +} + ? AggregateOfMap + : TAggregationRequest extends { aggregations?: AggregationMap } + ? AggregateOfMap + : {}; + +type SearchResponseOf< + TAggregationRequest extends TopLevelAggregationRequest, + TDocument +> = SubAggregateOf; + +// if aggregation response cannot be inferred, fall back to unknown +type WrapAggregationResponse = keyof T extends never + ? { aggregations?: unknown } + : { aggregations?: T }; + +export type InferSearchResponseOf< + TDocument = unknown, + TSearchRequest extends estypes.SearchRequest = estypes.SearchRequest, + TOptions extends { restTotalHitsAsInt?: boolean } = {} +> = Omit, 'aggregations' | 'hits'> & + (TSearchRequest['body'] extends TopLevelAggregationRequest + ? WrapAggregationResponse> + : { aggregations?: InvalidAggregationRequest }) & { + hits: Omit['hits'], 'total' | 'hits'> & + (TOptions['restTotalHitsAsInt'] extends true + ? { + total: number; + } + : { + total: { + value: number; + relation: 'eq' | 'gte'; + }; + }) & { hits: HitsOf }; + }; diff --git a/x-pack/plugins/actions/server/actions_client.test.ts b/x-pack/plugins/actions/server/actions_client.test.ts index 98b9b46fac48d6..a333d86b27129f 100644 --- a/x-pack/plugins/actions/server/actions_client.test.ts +++ b/x-pack/plugins/actions/server/actions_client.test.ts @@ -744,6 +744,7 @@ describe('getAll()', () => { }; unsecuredSavedObjectsClient.find.mockResolvedValueOnce(expectedResult); scopedClusterClient.asInternalUser.search.mockResolvedValueOnce( + // @ts-expect-error not full search response elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { '1': { doc_count: 6 }, @@ -817,6 +818,7 @@ describe('getAll()', () => { ], }); scopedClusterClient.asInternalUser.search.mockResolvedValueOnce( + // @ts-expect-error not full search response elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { '1': { doc_count: 6 }, @@ -877,6 +879,7 @@ describe('getAll()', () => { }; unsecuredSavedObjectsClient.find.mockResolvedValueOnce(expectedResult); scopedClusterClient.asInternalUser.search.mockResolvedValueOnce( + // @ts-expect-error not full search response elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { '1': { doc_count: 6 }, @@ -949,6 +952,7 @@ describe('getBulk()', () => { ], }); scopedClusterClient.asInternalUser.search.mockResolvedValueOnce( + // @ts-expect-error not full search response elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { '1': { doc_count: 6 }, @@ -1019,6 +1023,7 @@ describe('getBulk()', () => { ], }); scopedClusterClient.asInternalUser.search.mockResolvedValueOnce( + // @ts-expect-error not full search response elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { '1': { doc_count: 6 }, @@ -1076,6 +1081,7 @@ describe('getBulk()', () => { ], }); scopedClusterClient.asInternalUser.search.mockResolvedValueOnce( + // @ts-expect-error not full search response elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { '1': { doc_count: 6 }, diff --git a/x-pack/plugins/actions/server/actions_client.ts b/x-pack/plugins/actions/server/actions_client.ts index 2e2b3e7a6d8142..d8dcde2fab103c 100644 --- a/x-pack/plugins/actions/server/actions_client.ts +++ b/x-pack/plugins/actions/server/actions_client.ts @@ -6,6 +6,7 @@ */ import Boom from '@hapi/boom'; +import type { estypes } from '@elastic/elasticsearch'; import { i18n } from '@kbn/i18n'; import { omitBy, isUndefined } from 'lodash'; @@ -509,7 +510,7 @@ async function injectExtraFindData( scopedClusterClient: IScopedClusterClient, actionResults: ActionResult[] ): Promise { - const aggs: Record = {}; + const aggs: Record = {}; for (const actionResult of actionResults) { aggs[actionResult.id] = { filter: { @@ -555,6 +556,7 @@ async function injectExtraFindData( }); return actionResults.map((actionResult) => ({ ...actionResult, + // @ts-expect-error aggegation type is not specified referencedByCount: aggregationResult.aggregations[actionResult.id].doc_count, })); } diff --git a/x-pack/plugins/actions/server/builtin_action_types/es_index.ts b/x-pack/plugins/actions/server/builtin_action_types/es_index.ts index 72d2c09e984150..997c89b38a340e 100644 --- a/x-pack/plugins/actions/server/builtin_action_types/es_index.ts +++ b/x-pack/plugins/actions/server/builtin_action_types/es_index.ts @@ -93,8 +93,8 @@ async function executor( const err = find(result.items, 'index.error.reason'); if (err) { return wrapErr( - `${err.index.error!.reason}${ - err.index.error?.caused_by ? ` (${err.index.error?.caused_by?.reason})` : '' + `${err.index?.error?.reason}${ + err.index?.error?.caused_by ? ` (${err.index?.error?.caused_by?.reason})` : '' }`, actionId, logger diff --git a/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts b/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts index a998fc7af0c99c..e4611857ca2794 100644 --- a/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts +++ b/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts @@ -13,6 +13,7 @@ describe('actions telemetry', () => { test('getTotalCount should replace first symbol . to __ for action types names', async () => { const mockEsClient = elasticsearchClientMock.createClusterClient().asScoped().asInternalUser; mockEsClient.search.mockReturnValue( + // @ts-expect-error not full search response elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { byActionTypeId: { @@ -116,6 +117,7 @@ Object { test('getInUseTotalCount', async () => { const mockEsClient = elasticsearchClientMock.createClusterClient().asScoped().asInternalUser; mockEsClient.search.mockReturnValue( + // @ts-expect-error not full search response elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { refs: { diff --git a/x-pack/plugins/actions/server/usage/actions_telemetry.ts b/x-pack/plugins/actions/server/usage/actions_telemetry.ts index 6973a7e8dcbd29..8d028b176a00a6 100644 --- a/x-pack/plugins/actions/server/usage/actions_telemetry.ts +++ b/x-pack/plugins/actions/server/usage/actions_telemetry.ts @@ -53,21 +53,19 @@ export async function getTotalCount(esClient: ElasticsearchClient, kibanaIndex: }, }, }); - + // @ts-expect-error aggegation type is not specified + const aggs = searchResult.aggregations?.byActionTypeId.value?.types; return { - countTotal: Object.keys(searchResult.aggregations.byActionTypeId.value.types).reduce( - (total: number, key: string) => - parseInt(searchResult.aggregations.byActionTypeId.value.types[key], 0) + total, + countTotal: Object.keys(aggs).reduce( + (total: number, key: string) => parseInt(aggs[key], 0) + total, 0 ), - countByType: Object.keys(searchResult.aggregations.byActionTypeId.value.types).reduce( + countByType: Object.keys(aggs).reduce( // ES DSL aggregations are returned as `any` by esClient.search // eslint-disable-next-line @typescript-eslint/no-explicit-any (obj: any, key: string) => ({ ...obj, - [replaceFirstAndLastDotSymbols(key)]: searchResult.aggregations.byActionTypeId.value.types[ - key - ], + [replaceFirstAndLastDotSymbols(key)]: aggs[key], }), {} ), @@ -161,9 +159,9 @@ export async function getInUseTotalCount( }, }); - const bulkFilter = Object.entries( - actionResults.aggregations.refs.actionRefIds.value.connectorIds - ).map(([key]) => ({ + // @ts-expect-error aggegation type is not specified + const aggs = actionResults.aggregations.refs.actionRefIds.value; + const bulkFilter = Object.entries(aggs.connectorIds).map(([key]) => ({ id: key, type: 'action', fields: ['id', 'actionTypeId'], @@ -179,7 +177,7 @@ export async function getInUseTotalCount( }, {} ); - return { countTotal: actionResults.aggregations.refs.actionRefIds.value.total, countByType }; + return { countTotal: aggs.total, countByType }; } function replaceFirstAndLastDotSymbols(strToReplace: string) { diff --git a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts index 1b1075f4d7cf12..1b29191d9063e0 100644 --- a/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts +++ b/x-pack/plugins/alerting/server/alerts_client/alerts_client.ts @@ -8,6 +8,7 @@ import Boom from '@hapi/boom'; import { omit, isEqual, map, uniq, pick, truncate, trim } from 'lodash'; import { i18n } from '@kbn/i18n'; +import { estypes } from '@elastic/elasticsearch'; import { Logger, SavedObjectsClientContract, @@ -100,7 +101,7 @@ export interface FindOptions extends IndexType { defaultSearchOperator?: 'AND' | 'OR'; searchFields?: string[]; sortField?: string; - sortOrder?: string; + sortOrder?: estypes.SortOrder; hasReference?: { type: string; id: string; diff --git a/x-pack/plugins/alerting/server/usage/alerts_telemetry.test.ts b/x-pack/plugins/alerting/server/usage/alerts_telemetry.test.ts index 3c9decdf7ba96f..cce394d70ed6f1 100644 --- a/x-pack/plugins/alerting/server/usage/alerts_telemetry.test.ts +++ b/x-pack/plugins/alerting/server/usage/alerts_telemetry.test.ts @@ -13,6 +13,7 @@ describe('alerts telemetry', () => { test('getTotalCountInUse should replace first "." symbol to "__" in alert types names', async () => { const mockEsClient = elasticsearchClientMock.createClusterClient().asScoped().asInternalUser; mockEsClient.search.mockReturnValue( + // @ts-expect-error @elastic/elasticsearch Aggregate only allows unknown values elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { byAlertTypeId: { diff --git a/x-pack/plugins/alerting/server/usage/alerts_telemetry.ts b/x-pack/plugins/alerting/server/usage/alerts_telemetry.ts index 93bed31ce7d50a..46ac3e53895eba 100644 --- a/x-pack/plugins/alerting/server/usage/alerts_telemetry.ts +++ b/x-pack/plugins/alerting/server/usage/alerts_telemetry.ts @@ -246,50 +246,59 @@ export async function getTotalCountAggregations( }, }); - const totalAlertsCount = Object.keys(results.aggregations.byAlertTypeId.value.types).reduce( + const aggregations = results.aggregations as { + byAlertTypeId: { value: { types: Record } }; + throttleTime: { value: { min: number; max: number; totalCount: number; totalSum: number } }; + intervalTime: { value: { min: number; max: number; totalCount: number; totalSum: number } }; + connectorsAgg: { + connectors: { + value: { min: number; max: number; totalActionsCount: number; totalAlertsCount: number }; + }; + }; + }; + + const totalAlertsCount = Object.keys(aggregations.byAlertTypeId.value.types).reduce( (total: number, key: string) => - parseInt(results.aggregations.byAlertTypeId.value.types[key], 0) + total, + parseInt(aggregations.byAlertTypeId.value.types[key], 0) + total, 0 ); return { count_total: totalAlertsCount, - count_by_type: Object.keys(results.aggregations.byAlertTypeId.value.types).reduce( + count_by_type: Object.keys(aggregations.byAlertTypeId.value.types).reduce( // ES DSL aggregations are returned as `any` by esClient.search // eslint-disable-next-line @typescript-eslint/no-explicit-any (obj: any, key: string) => ({ ...obj, - [replaceFirstAndLastDotSymbols(key)]: results.aggregations.byAlertTypeId.value.types[key], + [replaceFirstAndLastDotSymbols(key)]: aggregations.byAlertTypeId.value.types[key], }), {} ), throttle_time: { - min: `${results.aggregations.throttleTime.value.min}s`, + min: `${aggregations.throttleTime.value.min}s`, avg: `${ - results.aggregations.throttleTime.value.totalCount > 0 - ? results.aggregations.throttleTime.value.totalSum / - results.aggregations.throttleTime.value.totalCount + aggregations.throttleTime.value.totalCount > 0 + ? aggregations.throttleTime.value.totalSum / aggregations.throttleTime.value.totalCount : 0 }s`, - max: `${results.aggregations.throttleTime.value.max}s`, + max: `${aggregations.throttleTime.value.max}s`, }, schedule_time: { - min: `${results.aggregations.intervalTime.value.min}s`, + min: `${aggregations.intervalTime.value.min}s`, avg: `${ - results.aggregations.intervalTime.value.totalCount > 0 - ? results.aggregations.intervalTime.value.totalSum / - results.aggregations.intervalTime.value.totalCount + aggregations.intervalTime.value.totalCount > 0 + ? aggregations.intervalTime.value.totalSum / aggregations.intervalTime.value.totalCount : 0 }s`, - max: `${results.aggregations.intervalTime.value.max}s`, + max: `${aggregations.intervalTime.value.max}s`, }, connectors_per_alert: { - min: results.aggregations.connectorsAgg.connectors.value.min, + min: aggregations.connectorsAgg.connectors.value.min, avg: totalAlertsCount > 0 - ? results.aggregations.connectorsAgg.connectors.value.totalActionsCount / totalAlertsCount + ? aggregations.connectorsAgg.connectors.value.totalActionsCount / totalAlertsCount : 0, - max: results.aggregations.connectorsAgg.connectors.value.max, + max: aggregations.connectorsAgg.connectors.value.max, }, }; } @@ -308,20 +317,23 @@ export async function getTotalCountInUse(esClient: ElasticsearchClient, kibanaIn }, }, }); + + const aggregations = searchResult.aggregations as { + byAlertTypeId: { value: { types: Record } }; + }; + return { - countTotal: Object.keys(searchResult.aggregations.byAlertTypeId.value.types).reduce( + countTotal: Object.keys(aggregations.byAlertTypeId.value.types).reduce( (total: number, key: string) => - parseInt(searchResult.aggregations.byAlertTypeId.value.types[key], 0) + total, + parseInt(aggregations.byAlertTypeId.value.types[key], 0) + total, 0 ), - countByType: Object.keys(searchResult.aggregations.byAlertTypeId.value.types).reduce( + countByType: Object.keys(aggregations.byAlertTypeId.value.types).reduce( // ES DSL aggregations are returned as `any` by esClient.search // eslint-disable-next-line @typescript-eslint/no-explicit-any (obj: any, key: string) => ({ ...obj, - [replaceFirstAndLastDotSymbols(key)]: searchResult.aggregations.byAlertTypeId.value.types[ - key - ], + [replaceFirstAndLastDotSymbols(key)]: aggregations.byAlertTypeId.value.types[key], }), {} ), diff --git a/x-pack/plugins/apm/public/components/app/RumDashboard/VisitorBreakdownMap/EmbeddedMap.test.tsx b/x-pack/plugins/apm/public/components/app/RumDashboard/VisitorBreakdownMap/EmbeddedMap.test.tsx index 4b925e914bfa57..f286f963b4fa0f 100644 --- a/x-pack/plugins/apm/public/components/app/RumDashboard/VisitorBreakdownMap/EmbeddedMap.test.tsx +++ b/x-pack/plugins/apm/public/components/app/RumDashboard/VisitorBreakdownMap/EmbeddedMap.test.tsx @@ -36,7 +36,7 @@ mockEmbeddable.getEmbeddableFactory = jest.fn().mockImplementation(() => ({ }), })); -const mockCore: () => [any] = () => { +const mockCore: () => any[] = () => { const core = { embeddable: mockEmbeddable, }; diff --git a/x-pack/plugins/apm/scripts/create-functional-tests-archive/index.ts b/x-pack/plugins/apm/scripts/create-functional-tests-archive/index.ts index 7eb6aba0d005ca..7f62fd39980606 100644 --- a/x-pack/plugins/apm/scripts/create-functional-tests-archive/index.ts +++ b/x-pack/plugins/apm/scripts/create-functional-tests-archive/index.ts @@ -10,6 +10,7 @@ import { execSync } from 'child_process'; import moment from 'moment'; import path from 'path'; import fs from 'fs'; +import { QueryContainer } from '@elastic/elasticsearch/api/types'; import { getEsClient } from '../shared/get_es_client'; import { parseIndexUrl } from '../shared/parse_index_url'; @@ -116,7 +117,7 @@ async function run() { const query = { bool: { - should: should.map(({ bool }) => ({ bool })), + should: should.map(({ bool }) => ({ bool })) as QueryContainer[], minimum_should_match: 1, }, }; diff --git a/x-pack/plugins/apm/scripts/upload-telemetry-data/index.ts b/x-pack/plugins/apm/scripts/upload-telemetry-data/index.ts index e4aedf452002dd..25554eeeaf81d9 100644 --- a/x-pack/plugins/apm/scripts/upload-telemetry-data/index.ts +++ b/x-pack/plugins/apm/scripts/upload-telemetry-data/index.ts @@ -13,7 +13,6 @@ // - Validate whether we can run the queries we want to on the telemetry data import { merge, chunk, flatten, omit } from 'lodash'; -import { Client } from '@elastic/elasticsearch'; import { argv } from 'yargs'; import { Logger } from 'kibana/server'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths @@ -26,6 +25,7 @@ import { generateSampleDocuments } from './generate-sample-documents'; import { readKibanaConfig } from '../shared/read-kibana-config'; import { getHttpAuth } from '../shared/get-http-auth'; import { createOrUpdateIndex } from '../shared/create-or-update-index'; +import { getEsClient } from '../shared/get_es_client'; async function uploadData() { const githubToken = process.env.GITHUB_TOKEN; @@ -43,8 +43,8 @@ async function uploadData() { const httpAuth = getHttpAuth(config); - const client = new Client({ - nodes: [config['elasticsearch.hosts']], + const client = getEsClient({ + node: config['elasticsearch.hosts'], ...(httpAuth ? { auth: { ...httpAuth, username: 'elastic' }, @@ -83,10 +83,10 @@ async function uploadData() { apmAgentConfigurationIndex: '.apm-agent-configuration', }, search: (body) => { - return unwrapEsResponse(client.search(body as any)); + return unwrapEsResponse(client.search(body)) as Promise; }, indicesStats: (body) => { - return unwrapEsResponse(client.indices.stats(body)); + return unwrapEsResponse(client.indices.stats(body)); }, transportRequest: ((params) => { return unwrapEsResponse( diff --git a/x-pack/plugins/apm/server/lib/alerts/alerting_es_client.ts b/x-pack/plugins/apm/server/lib/alerts/alerting_es_client.ts index c4fef64f515d1e..9a0ba514bb4794 100644 --- a/x-pack/plugins/apm/server/lib/alerts/alerting_es_client.ts +++ b/x-pack/plugins/apm/server/lib/alerts/alerting_es_client.ts @@ -25,8 +25,8 @@ export function alertingEsClient( >, params: TParams ): Promise>> { - return services.scopedClusterClient.asCurrentUser.search({ + return (services.scopedClusterClient.asCurrentUser.search({ ...params, ignore_unavailable: true, - }); + }) as unknown) as Promise>>; } diff --git a/x-pack/plugins/apm/server/lib/alerts/chart_preview/get_transaction_duration.ts b/x-pack/plugins/apm/server/lib/alerts/chart_preview/get_transaction_duration.ts index bea90109725d0c..508b9419344cde 100644 --- a/x-pack/plugins/apm/server/lib/alerts/chart_preview/get_transaction_duration.ts +++ b/x-pack/plugins/apm/server/lib/alerts/chart_preview/get_transaction_duration.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { MetricsAggregationResponsePart } from '../../../../../../../typings/elasticsearch/aggregations'; +import { QueryContainer } from '@elastic/elasticsearch/api/types'; import { PROCESSOR_EVENT, SERVICE_NAME, @@ -45,7 +45,7 @@ export function getTransactionDurationChartPreview({ : []), ...rangeQuery(start, end), ...environmentQuery(environment), - ], + ] as QueryContainer[], }, }; @@ -85,7 +85,7 @@ export function getTransactionDurationChartPreview({ const x = bucket.key; const y = aggregationType === 'avg' - ? (bucket.agg as MetricsAggregationResponsePart).value + ? (bucket.agg as { value: number | null }).value : (bucket.agg as { values: Record }).values[ percentilesKey ]; diff --git a/x-pack/plugins/apm/server/lib/alerts/register_error_count_alert_type.test.ts b/x-pack/plugins/apm/server/lib/alerts/register_error_count_alert_type.test.ts index 167cb133102f26..d7dd7aee3ca25e 100644 --- a/x-pack/plugins/apm/server/lib/alerts/register_error_count_alert_type.test.ts +++ b/x-pack/plugins/apm/server/lib/alerts/register_error_count_alert_type.test.ts @@ -54,10 +54,20 @@ describe('Error count alert', () => { services.scopedClusterClient.asCurrentUser.search.mockReturnValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ hits: { + hits: [], total: { + relation: 'eq', value: 0, }, }, + took: 0, + timed_out: false, + _shards: { + failed: 0, + skipped: 0, + successful: 1, + total: 1, + }, }) ); @@ -89,7 +99,9 @@ describe('Error count alert', () => { services.scopedClusterClient.asCurrentUser.search.mockReturnValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ hits: { + hits: [], total: { + relation: 'eq', value: 2, }, }, @@ -111,6 +123,14 @@ describe('Error count alert', () => { ], }, }, + took: 0, + timed_out: false, + _shards: { + failed: 0, + skipped: 0, + successful: 1, + total: 1, + }, }) ); @@ -177,7 +197,9 @@ describe('Error count alert', () => { services.scopedClusterClient.asCurrentUser.search.mockReturnValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ hits: { + hits: [], total: { + relation: 'eq', value: 2, }, }, @@ -193,6 +215,14 @@ describe('Error count alert', () => { ], }, }, + took: 0, + timed_out: false, + _shards: { + failed: 0, + skipped: 0, + successful: 1, + total: 1, + }, }) ); diff --git a/x-pack/plugins/apm/server/lib/alerts/register_transaction_error_rate_alert_type.test.ts b/x-pack/plugins/apm/server/lib/alerts/register_transaction_error_rate_alert_type.test.ts index c18f29b6267e08..148cd813a8a220 100644 --- a/x-pack/plugins/apm/server/lib/alerts/register_transaction_error_rate_alert_type.test.ts +++ b/x-pack/plugins/apm/server/lib/alerts/register_transaction_error_rate_alert_type.test.ts @@ -52,10 +52,20 @@ describe('Transaction error rate alert', () => { services.scopedClusterClient.asCurrentUser.search.mockReturnValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ hits: { + hits: [], total: { + relation: 'eq', value: 0, }, }, + took: 0, + timed_out: false, + _shards: { + failed: 0, + skipped: 0, + successful: 1, + total: 1, + }, }) ); @@ -87,7 +97,9 @@ describe('Transaction error rate alert', () => { services.scopedClusterClient.asCurrentUser.search.mockReturnValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ hits: { + hits: [], total: { + relation: 'eq', value: 4, }, }, @@ -126,6 +138,14 @@ describe('Transaction error rate alert', () => { ], }, }, + took: 0, + timed_out: false, + _shards: { + failed: 0, + skipped: 0, + successful: 1, + total: 1, + }, }) ); @@ -196,7 +216,9 @@ describe('Transaction error rate alert', () => { services.scopedClusterClient.asCurrentUser.search.mockReturnValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ hits: { + hits: [], total: { + relation: 'eq', value: 4, }, }, @@ -221,6 +243,14 @@ describe('Transaction error rate alert', () => { ], }, }, + took: 0, + timed_out: false, + _shards: { + failed: 0, + skipped: 0, + successful: 1, + total: 1, + }, }) ); @@ -274,8 +304,10 @@ describe('Transaction error rate alert', () => { services.scopedClusterClient.asCurrentUser.search.mockReturnValue( elasticsearchClientMock.createSuccessTransportRequestPromise({ hits: { + hits: [], total: { value: 4, + relation: 'eq', }, }, aggregations: { @@ -286,6 +318,14 @@ describe('Transaction error rate alert', () => { buckets: [{ key: 'foo' }, { key: 'bar' }], }, }, + took: 0, + timed_out: false, + _shards: { + failed: 0, + skipped: 0, + successful: 1, + total: 1, + }, }) ); diff --git a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/index.ts b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/index.ts index 98063e3e1e3fd1..87686d2c30cae4 100644 --- a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/index.ts +++ b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/index.ts @@ -7,7 +7,7 @@ import { merge } from 'lodash'; import { Logger } from 'kibana/server'; -import { RequestParams } from '@elastic/elasticsearch'; +import { IndicesStats } from '@elastic/elasticsearch/api/requestParams'; import { ESSearchRequest, ESSearchResponse, @@ -22,7 +22,7 @@ type TelemetryTaskExecutor = (params: { params: TSearchRequest ): Promise>; indicesStats( - params: RequestParams.IndicesStats + params: IndicesStats // promise returned by client has an abort property // so we cannot use its ReturnType ): Promise<{ diff --git a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts index e9744c6614641b..e2a39b521466a4 100644 --- a/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts +++ b/x-pack/plugins/apm/server/lib/apm_telemetry/collect_data_telemetry/tasks.ts @@ -4,10 +4,9 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - -import { ValuesType } from 'utility-types'; import { flatten, merge, sortBy, sum, pickBy } from 'lodash'; -import { AggregationOptionsByType } from '../../../../../../../typings/elasticsearch/aggregations'; +import { CompositeAggregationSource } from '@elastic/elasticsearch/api/types'; +import { asMutableArray } from '../../../../common/utils/as_mutable_array'; import { ProcessorEvent } from '../../../../common/processor_event'; import { TelemetryTask } from '.'; import { AGENT_NAMES, RUM_AGENT_NAMES } from '../../../../common/agent_name'; @@ -60,9 +59,7 @@ export const tasks: TelemetryTask[] = [ // the transaction count for that time range. executor: async ({ indices, search }) => { async function getBucketCountFromPaginatedQuery( - sources: Array< - ValuesType[string] - >, + sources: CompositeAggregationSource[], prevResult?: { transaction_count: number; expected_metric_document_count: number; @@ -151,7 +148,7 @@ export const tasks: TelemetryTask[] = [ }, size: 1, sort: { - '@timestamp': 'desc', + '@timestamp': 'desc' as const, }, }, }) @@ -317,7 +314,7 @@ export const tasks: TelemetryTask[] = [ service_environments: { composite: { size: 1000, - sources: [ + sources: asMutableArray([ { [SERVICE_ENVIRONMENT]: { terms: { @@ -333,7 +330,7 @@ export const tasks: TelemetryTask[] = [ }, }, }, - ], + ] as const), }, }, }, diff --git a/x-pack/plugins/apm/server/lib/apm_telemetry/index.ts b/x-pack/plugins/apm/server/lib/apm_telemetry/index.ts index 848da41bc8372b..88ef1203bae9ff 100644 --- a/x-pack/plugins/apm/server/lib/apm_telemetry/index.ts +++ b/x-pack/plugins/apm/server/lib/apm_telemetry/index.ts @@ -76,7 +76,7 @@ export async function createApmTelemetry({ }); const search: CollectTelemetryParams['search'] = (params) => - unwrapEsResponse(esClient.asInternalUser.search(params)); + unwrapEsResponse(esClient.asInternalUser.search(params)) as any; const indicesStats: CollectTelemetryParams['indicesStats'] = (params) => unwrapEsResponse(esClient.asInternalUser.indices.stats(params)); diff --git a/x-pack/plugins/apm/server/lib/correlations/get_correlations_for_failed_transactions/index.ts b/x-pack/plugins/apm/server/lib/correlations/get_correlations_for_failed_transactions/index.ts index f613a0dbca4025..c668f3bb287138 100644 --- a/x-pack/plugins/apm/server/lib/correlations/get_correlations_for_failed_transactions/index.ts +++ b/x-pack/plugins/apm/server/lib/correlations/get_correlations_for_failed_transactions/index.ts @@ -11,7 +11,7 @@ import { processSignificantTermAggs, TopSigTerm, } from '../process_significant_term_aggs'; -import { AggregationOptionsByType } from '../../../../../../../typings/elasticsearch/aggregations'; +import { AggregationOptionsByType } from '../../../../../../../typings/elasticsearch'; import { ESFilter } from '../../../../../../../typings/elasticsearch'; import { environmentQuery, diff --git a/x-pack/plugins/apm/server/lib/correlations/get_correlations_for_slow_transactions/get_latency_distribution.ts b/x-pack/plugins/apm/server/lib/correlations/get_correlations_for_slow_transactions/get_latency_distribution.ts index b800a21ffc3413..50613c10ff7c0d 100644 --- a/x-pack/plugins/apm/server/lib/correlations/get_correlations_for_slow_transactions/get_latency_distribution.ts +++ b/x-pack/plugins/apm/server/lib/correlations/get_correlations_for_slow_transactions/get_latency_distribution.ts @@ -6,7 +6,7 @@ */ import { isEmpty, dropRightWhile } from 'lodash'; -import { AggregationOptionsByType } from '../../../../../../../typings/elasticsearch/aggregations'; +import { AggregationOptionsByType } from '../../../../../../../typings/elasticsearch'; import { ESFilter } from '../../../../../../../typings/elasticsearch'; import { TRANSACTION_DURATION } from '../../../../common/elasticsearch_fieldnames'; import { ProcessorEvent } from '../../../../common/processor_event'; diff --git a/x-pack/plugins/apm/server/lib/correlations/get_correlations_for_slow_transactions/index.ts b/x-pack/plugins/apm/server/lib/correlations/get_correlations_for_slow_transactions/index.ts index 6afca46ec7391e..9472d385a26c60 100644 --- a/x-pack/plugins/apm/server/lib/correlations/get_correlations_for_slow_transactions/index.ts +++ b/x-pack/plugins/apm/server/lib/correlations/get_correlations_for_slow_transactions/index.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { AggregationOptionsByType } from '../../../../../../../typings/elasticsearch/aggregations'; +import { AggregationOptionsByType } from '../../../../../../../typings/elasticsearch'; import { ESFilter } from '../../../../../../../typings/elasticsearch'; import { environmentQuery, @@ -73,6 +73,10 @@ export async function getCorrelationsForSlowTransactions({ setup, }); + if (!durationForPercentile) { + return {}; + } + const response = await withApmSpan('get_significant_terms', () => { const params = { apm: { events: [ProcessorEvent.transaction] }, diff --git a/x-pack/plugins/apm/server/lib/correlations/process_significant_term_aggs.ts b/x-pack/plugins/apm/server/lib/correlations/process_significant_term_aggs.ts index 2732cd45c342e7..94ed3dc3b69996 100644 --- a/x-pack/plugins/apm/server/lib/correlations/process_significant_term_aggs.ts +++ b/x-pack/plugins/apm/server/lib/correlations/process_significant_term_aggs.ts @@ -9,7 +9,7 @@ import { orderBy } from 'lodash'; import { AggregationOptionsByType, AggregationResultOf, -} from '../../../../../../typings/elasticsearch/aggregations'; +} from '../../../../../../typings/elasticsearch'; export interface TopSigTerm { fieldName: string; diff --git a/x-pack/plugins/apm/server/lib/errors/get_error_group_sample.ts b/x-pack/plugins/apm/server/lib/errors/get_error_group_sample.ts index d4ad2c8a9b2cbb..57fb4861809932 100644 --- a/x-pack/plugins/apm/server/lib/errors/get_error_group_sample.ts +++ b/x-pack/plugins/apm/server/lib/errors/get_error_group_sample.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { asMutableArray } from '../../../common/utils/as_mutable_array'; import { ERROR_GROUP_ID, SERVICE_NAME, @@ -54,10 +55,10 @@ export function getErrorGroupSample({ should: [{ term: { [TRANSACTION_SAMPLED]: true } }], }, }, - sort: [ + sort: asMutableArray([ { _score: 'desc' }, // sort by _score first to ensure that errors with transaction.sampled:true ends up on top { '@timestamp': { order: 'desc' } }, // sort by timestamp to get the most recent error - ], + ] as const), }, }; diff --git a/x-pack/plugins/apm/server/lib/errors/get_error_groups.ts b/x-pack/plugins/apm/server/lib/errors/get_error_groups.ts index 1c262ebf882b2e..f5b22e5349756c 100644 --- a/x-pack/plugins/apm/server/lib/errors/get_error_groups.ts +++ b/x-pack/plugins/apm/server/lib/errors/get_error_groups.ts @@ -5,7 +5,6 @@ * 2.0. */ -import { SortOptions } from '../../../../../../typings/elasticsearch/aggregations'; import { ERROR_CULPRIT, ERROR_EXC_HANDLED, @@ -48,7 +47,7 @@ export function getErrorGroups({ serviceName, }); - const order: SortOptions = sortByLatestOccurrence + const order = sortByLatestOccurrence ? { max_timestamp: sortDirection, } diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts index e04b3a70a75933..e20103cc6ddca1 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts @@ -13,7 +13,7 @@ import { } from '../../../../../../../../src/core/server'; import { ESSearchRequest, - ESSearchResponse, + InferSearchResponseOf, } from '../../../../../../../../typings/elasticsearch'; import { unwrapEsResponse } from '../../../../../../observability/server'; import { ProcessorEvent } from '../../../../../common/processor_event'; @@ -54,7 +54,7 @@ type ESSearchRequestOf = Omit< type TypedSearchResponse< TParams extends APMEventESSearchRequest -> = ESSearchResponse< +> = InferSearchResponseOf< TypeOfProcessorEvent>, ESSearchRequestOf >; diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_internal_es_client/index.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_internal_es_client/index.ts index 4faf80d7ca8db8..2e83baece01a9f 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_internal_es_client/index.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_internal_es_client/index.ts @@ -6,8 +6,12 @@ */ import { KibanaRequest } from 'src/core/server'; -import { RequestParams } from '@elastic/elasticsearch'; import { TransportRequestPromise } from '@elastic/elasticsearch/lib/Transport'; +import { + CreateIndexRequest, + DeleteRequest, + IndexRequest, +} from '@elastic/elasticsearch/api/types'; import { unwrapEsResponse } from '../../../../../../observability/server'; import { APMRequestHandlerContext } from '../../../../routes/typings'; import { @@ -21,7 +25,7 @@ import { } from '../call_async_with_debug'; import { cancelEsRequestOnAbort } from '../cancel_es_request_on_abort'; -export type APMIndexDocumentParams = RequestParams.Index; +export type APMIndexDocumentParams = IndexRequest; export type APMInternalClient = ReturnType; @@ -73,14 +77,14 @@ export function createInternalESClient({ params, }); }, - delete: (params: RequestParams.Delete): Promise<{ result: string }> => { + delete: (params: DeleteRequest): Promise<{ result: string }> => { return callEs({ operationName: 'delete', cb: () => asInternalUser.delete(params), params, }); }, - indicesCreate: (params: RequestParams.IndicesCreate) => { + indicesCreate: (params: CreateIndexRequest) => { return callEs({ operationName: 'indices.create', cb: () => asInternalUser.indices.create(params), diff --git a/x-pack/plugins/apm/server/lib/helpers/setup_request.test.ts b/x-pack/plugins/apm/server/lib/helpers/setup_request.test.ts index 30b81a15f5efa5..51f386d59c04ab 100644 --- a/x-pack/plugins/apm/server/lib/helpers/setup_request.test.ts +++ b/x-pack/plugins/apm/server/lib/helpers/setup_request.test.ts @@ -157,7 +157,9 @@ describe('setupRequest', () => { apm: { events: [ProcessorEvent.transaction], }, - body: { query: { bool: { filter: [{ term: 'someTerm' }] } } }, + body: { + query: { bool: { filter: [{ term: { field: 'someTerm' } }] } }, + }, }); const params = mockContext.core.elasticsearch.client.asCurrentUser.search.mock @@ -166,7 +168,7 @@ describe('setupRequest', () => { query: { bool: { filter: [ - { term: 'someTerm' }, + { term: { field: 'someTerm' } }, { terms: { [PROCESSOR_EVENT]: ['transaction'] } }, { range: { 'observer.version_major': { gte: 7 } } }, ], @@ -183,7 +185,9 @@ describe('setupRequest', () => { apm: { events: [ProcessorEvent.error], }, - body: { query: { bool: { filter: [{ term: 'someTerm' }] } } }, + body: { + query: { bool: { filter: [{ term: { field: 'someTerm' } }] } }, + }, }, { includeLegacyData: true, @@ -196,7 +200,7 @@ describe('setupRequest', () => { query: { bool: { filter: [ - { term: 'someTerm' }, + { term: { field: 'someTerm' } }, { terms: { [PROCESSOR_EVENT]: ['error'], diff --git a/x-pack/plugins/apm/server/lib/helpers/transaction_error_rate.ts b/x-pack/plugins/apm/server/lib/helpers/transaction_error_rate.ts index 9bec5eb4a247c6..11d65b7697e9a8 100644 --- a/x-pack/plugins/apm/server/lib/helpers/transaction_error_rate.ts +++ b/x-pack/plugins/apm/server/lib/helpers/transaction_error_rate.ts @@ -10,7 +10,7 @@ import { EventOutcome } from '../../../common/event_outcome'; import { AggregationOptionsByType, AggregationResultOf, -} from '../../../../../../typings/elasticsearch/aggregations'; +} from '../../../../../../typings/elasticsearch'; export const getOutcomeAggregation = () => ({ terms: { diff --git a/x-pack/plugins/apm/server/lib/metrics/by_agent/java/gc/fetch_and_transform_gc_metrics.ts b/x-pack/plugins/apm/server/lib/metrics/by_agent/java/gc/fetch_and_transform_gc_metrics.ts index 9f83af989fc571..3b3ef8b9c4bcf5 100644 --- a/x-pack/plugins/apm/server/lib/metrics/by_agent/java/gc/fetch_and_transform_gc_metrics.ts +++ b/x-pack/plugins/apm/server/lib/metrics/by_agent/java/gc/fetch_and_transform_gc_metrics.ts @@ -7,6 +7,7 @@ import { sum, round } from 'lodash'; import theme from '@elastic/eui/dist/eui_theme_light.json'; +import { isFiniteNumber } from '../../../../../../common/utils/is_finite_number'; import { Setup, SetupTimeRange } from '../../../../helpers/setup_request'; import { getMetricsDateHistogramParams } from '../../../../helpers/metrics'; import { ChartBase } from '../../../types'; @@ -126,10 +127,9 @@ export async function fetchAndTransformGcMetrics({ const data = timeseriesData.buckets.map((bucket) => { // derivative/value will be undefined for the first hit and if the `max` value is null const bucketValue = bucket.value?.value; - const y = - bucketValue !== null && bucketValue !== undefined && bucket.value - ? round(bucketValue * (60 / bucketSize), 1) - : null; + const y = isFiniteNumber(bucketValue) + ? round(bucketValue * (60 / bucketSize), 1) + : null; return { y, diff --git a/x-pack/plugins/apm/server/lib/rum_client/get_page_load_distribution.ts b/x-pack/plugins/apm/server/lib/rum_client/get_page_load_distribution.ts index df51bf30c1a53d..0f1d7146f84596 100644 --- a/x-pack/plugins/apm/server/lib/rum_client/get_page_load_distribution.ts +++ b/x-pack/plugins/apm/server/lib/rum_client/get_page_load_distribution.ts @@ -144,12 +144,16 @@ export async function getPageLoadDistribution({ } // calculate the diff to get actual page load on specific duration value - let pageDist = pageDistVals.map(({ key, value }, index: number, arr) => { - return { - x: microToSec(key), - y: index === 0 ? value : value - arr[index - 1].value, - }; - }); + let pageDist = pageDistVals.map( + ({ key, value: maybeNullValue }, index: number, arr) => { + // FIXME: values from percentile* aggs can be null + const value = maybeNullValue!; + return { + x: microToSec(key), + y: index === 0 ? value : value - arr[index - 1].value!, + }; + } + ); pageDist = removeZeroesFromTail(pageDist); diff --git a/x-pack/plugins/apm/server/lib/rum_client/get_pl_dist_breakdown.ts b/x-pack/plugins/apm/server/lib/rum_client/get_pl_dist_breakdown.ts index 12b7961d516101..6a6caab9537333 100644 --- a/x-pack/plugins/apm/server/lib/rum_client/get_pl_dist_breakdown.ts +++ b/x-pack/plugins/apm/server/lib/rum_client/get_pl_dist_breakdown.ts @@ -98,10 +98,12 @@ export const getPageLoadDistBreakdown = async ({ return pageDistBreakdowns?.map(({ key, page_dist: pageDist }) => { let seriesData = pageDist.values?.map( - ({ key: pKey, value }, index: number, arr) => { + ({ key: pKey, value: maybeNullValue }, index: number, arr) => { + // FIXME: values from percentile* aggs can be null + const value = maybeNullValue!; return { x: microToSec(pKey), - y: index === 0 ? value : value - arr[index - 1].value, + y: index === 0 ? value : value - arr[index - 1].value!, }; } ); diff --git a/x-pack/plugins/apm/server/lib/rum_client/get_web_core_vitals.ts b/x-pack/plugins/apm/server/lib/rum_client/get_web_core_vitals.ts index 88228f33dd3af6..9bde701df56721 100644 --- a/x-pack/plugins/apm/server/lib/rum_client/get_web_core_vitals.ts +++ b/x-pack/plugins/apm/server/lib/rum_client/get_web_core_vitals.ts @@ -117,7 +117,7 @@ export async function getWebCoreVitals({ } = response.aggregations ?? {}; const getRanksPercentages = ( - ranks?: Array<{ key: number; value: number }> + ranks?: Array<{ key: number; value: number | null }> ) => { const ranksVal = ranks?.map(({ value }) => value?.toFixed(0) ?? 0) ?? []; return [ diff --git a/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts b/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts index 8c97a3993e8c09..bcddbff34a8f64 100644 --- a/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts +++ b/x-pack/plugins/apm/server/lib/service_map/get_service_anomalies.ts @@ -7,6 +7,7 @@ import Boom from '@hapi/boom'; import { sortBy, uniqBy } from 'lodash'; +import { estypes } from '@elastic/elasticsearch'; import { ESSearchResponse } from '../../../../../../typings/elasticsearch'; import { MlPluginSetup } from '../../../../ml/server'; import { PromiseReturnType } from '../../../../observability/typings/common'; @@ -63,7 +64,7 @@ export async function getServiceAnomalies({ by_field_value: [TRANSACTION_REQUEST, TRANSACTION_PAGE_LOAD], }, }, - ], + ] as estypes.QueryContainer[], }, }, aggs: { @@ -73,7 +74,7 @@ export async function getServiceAnomalies({ sources: [ { serviceName: { terms: { field: 'partition_field_value' } } }, { jobId: { terms: { field: 'job_id' } } }, - ], + ] as Array>, }, aggs: { metrics: { @@ -83,7 +84,7 @@ export async function getServiceAnomalies({ { field: 'by_field_value' }, { field: 'result_type' }, { field: 'record_score' }, - ] as const, + ], sort: { record_score: 'desc' as const, }, diff --git a/x-pack/plugins/apm/server/lib/service_map/get_trace_sample_ids.ts b/x-pack/plugins/apm/server/lib/service_map/get_trace_sample_ids.ts index 8bc1b1f0562f54..fa04b963388b20 100644 --- a/x-pack/plugins/apm/server/lib/service_map/get_trace_sample_ids.ts +++ b/x-pack/plugins/apm/server/lib/service_map/get_trace_sample_ids.ts @@ -7,6 +7,7 @@ import Boom from '@hapi/boom'; import { sortBy, take, uniq } from 'lodash'; +import { asMutableArray } from '../../../common/utils/as_mutable_array'; import { ESFilter } from '../../../../../../typings/elasticsearch'; import { SERVICE_ENVIRONMENT, @@ -73,7 +74,7 @@ export function getTraceSampleIds({ aggs: { connections: { composite: { - sources: [ + sources: asMutableArray([ { [SPAN_DESTINATION_SERVICE_RESOURCE]: { terms: { @@ -96,7 +97,7 @@ export function getTraceSampleIds({ }, }, }, - ], + ] as const), size: fingerprintBucketSize, }, aggs: { diff --git a/x-pack/plugins/apm/server/lib/services/__snapshots__/queries.test.ts.snap b/x-pack/plugins/apm/server/lib/services/__snapshots__/queries.test.ts.snap index 7e7e073c0d2f6e..9d05369aca840f 100644 --- a/x-pack/plugins/apm/server/lib/services/__snapshots__/queries.test.ts.snap +++ b/x-pack/plugins/apm/server/lib/services/__snapshots__/queries.test.ts.snap @@ -131,9 +131,11 @@ Array [ }, "sample": Object { "top_metrics": Object { - "metrics": Object { - "field": "agent.name", - }, + "metrics": Array [ + Object { + "field": "agent.name", + }, + ], "sort": Object { "@timestamp": "desc", }, @@ -213,9 +215,11 @@ Array [ }, "latest": Object { "top_metrics": Object { - "metrics": Object { - "field": "agent.name", - }, + "metrics": Array [ + Object { + "field": "agent.name", + }, + ], "sort": Object { "@timestamp": "desc", }, diff --git a/x-pack/plugins/apm/server/lib/services/annotations/get_stored_annotations.ts b/x-pack/plugins/apm/server/lib/services/annotations/get_stored_annotations.ts index e0329e5f60e198..3e1a8f26de6b42 100644 --- a/x-pack/plugins/apm/server/lib/services/annotations/get_stored_annotations.ts +++ b/x-pack/plugins/apm/server/lib/services/annotations/get_stored_annotations.ts @@ -57,17 +57,17 @@ export function getStoredAnnotations({ const response: ESSearchResponse< ESAnnotation, { body: typeof body } - > = await unwrapEsResponse( - client.search({ + > = await (unwrapEsResponse( + client.search({ index: annotationsClient.index, body, }) - ); + ) as any); return response.hits.hits.map((hit) => { return { type: AnnotationType.VERSION, - id: hit._id, + id: hit._id as string, '@timestamp': new Date(hit._source['@timestamp']).getTime(), text: hit._source.message, }; diff --git a/x-pack/plugins/apm/server/lib/services/get_service_dependencies/get_destination_map.ts b/x-pack/plugins/apm/server/lib/services/get_service_dependencies/get_destination_map.ts index cb9d37d56b8678..8a099683b371bf 100644 --- a/x-pack/plugins/apm/server/lib/services/get_service_dependencies/get_destination_map.ts +++ b/x-pack/plugins/apm/server/lib/services/get_service_dependencies/get_destination_map.ts @@ -6,6 +6,7 @@ */ import { isEqual, keyBy, mapValues } from 'lodash'; +import { asMutableArray } from '../../../../common/utils/as_mutable_array'; import { pickKeys } from '../../../../common/utils/pick_keys'; import { AgentName } from '../../../../typings/es_schemas/ui/fields/agent'; import { @@ -58,7 +59,7 @@ export const getDestinationMap = ({ connections: { composite: { size: 1000, - sources: [ + sources: asMutableArray([ { [SPAN_DESTINATION_SERVICE_RESOURCE]: { terms: { field: SPAN_DESTINATION_SERVICE_RESOURCE }, @@ -67,7 +68,7 @@ export const getDestinationMap = ({ // make sure we get samples for both successful // and failed calls { [EVENT_OUTCOME]: { terms: { field: EVENT_OUTCOME } } }, - ], + ] as const), }, aggs: { sample: { @@ -76,9 +77,9 @@ export const getDestinationMap = ({ { field: SPAN_TYPE }, { field: SPAN_SUBTYPE }, { field: SPAN_ID }, - ] as const, + ], sort: { - '@timestamp': 'desc', + '@timestamp': 'desc' as const, }, }, }, @@ -126,12 +127,12 @@ export const getDestinationMap = ({ }, }, size: outgoingConnections.length, - docvalue_fields: [ + docvalue_fields: asMutableArray([ SERVICE_NAME, SERVICE_ENVIRONMENT, AGENT_NAME, PARENT_ID, - ] as const, + ] as const), _source: false, }, }) diff --git a/x-pack/plugins/apm/server/lib/services/get_service_error_groups/index.ts b/x-pack/plugins/apm/server/lib/services/get_service_error_groups/index.ts index 676ba1625cc612..7729822df30ca0 100644 --- a/x-pack/plugins/apm/server/lib/services/get_service_error_groups/index.ts +++ b/x-pack/plugins/apm/server/lib/services/get_service_error_groups/index.ts @@ -90,11 +90,11 @@ export async function getServiceErrorGroups({ sample: { top_hits: { size: 1, - _source: [ + _source: ([ ERROR_LOG_MESSAGE, ERROR_EXC_MESSAGE, '@timestamp', - ], + ] as any) as string, sort: { '@timestamp': 'desc', }, diff --git a/x-pack/plugins/apm/server/lib/services/get_service_metadata_details.ts b/x-pack/plugins/apm/server/lib/services/get_service_metadata_details.ts index a71772d1429cb0..e2341b306a8781 100644 --- a/x-pack/plugins/apm/server/lib/services/get_service_metadata_details.ts +++ b/x-pack/plugins/apm/server/lib/services/get_service_metadata_details.ts @@ -6,7 +6,6 @@ */ import { ProcessorEvent } from '../../../common/processor_event'; -import { SortOptions } from '../../../../../../typings/elasticsearch'; import { AGENT, CLOUD, @@ -96,7 +95,7 @@ export function getServiceMetadataDetails({ terms: { field: SERVICE_VERSION, size: 10, - order: { _key: 'desc' } as SortOptions, + order: { _key: 'desc' as const }, }, }, availabilityZones: { diff --git a/x-pack/plugins/apm/server/lib/services/get_services/get_service_transaction_stats.ts b/x-pack/plugins/apm/server/lib/services/get_services/get_service_transaction_stats.ts index 10c7420d0f3b04..1e36df379e9646 100644 --- a/x-pack/plugins/apm/server/lib/services/get_services/get_service_transaction_stats.ts +++ b/x-pack/plugins/apm/server/lib/services/get_services/get_service_transaction_stats.ts @@ -108,9 +108,9 @@ export async function getServiceTransactionStats({ }, sample: { top_metrics: { - metrics: { field: AGENT_NAME } as const, + metrics: [{ field: AGENT_NAME } as const], sort: { - '@timestamp': 'desc', + '@timestamp': 'desc' as const, }, }, }, diff --git a/x-pack/plugins/apm/server/lib/services/get_services/get_services_from_metric_documents.ts b/x-pack/plugins/apm/server/lib/services/get_services/get_services_from_metric_documents.ts index cabd44c1e6907a..906cc62e64d1a4 100644 --- a/x-pack/plugins/apm/server/lib/services/get_services/get_services_from_metric_documents.ts +++ b/x-pack/plugins/apm/server/lib/services/get_services/get_services_from_metric_documents.ts @@ -59,7 +59,7 @@ export function getServicesFromMetricDocuments({ }, latest: { top_metrics: { - metrics: { field: AGENT_NAME } as const, + metrics: [{ field: AGENT_NAME } as const], sort: { '@timestamp': 'desc' }, }, }, diff --git a/x-pack/plugins/apm/server/lib/settings/agent_configuration/convert_settings_to_string.ts b/x-pack/plugins/apm/server/lib/settings/agent_configuration/convert_settings_to_string.ts index 2d6eff33b5b4e0..0b826ea10b6c4f 100644 --- a/x-pack/plugins/apm/server/lib/settings/agent_configuration/convert_settings_to_string.ts +++ b/x-pack/plugins/apm/server/lib/settings/agent_configuration/convert_settings_to_string.ts @@ -5,13 +5,13 @@ * 2.0. */ -import { ESSearchHit } from '../../../../../../../typings/elasticsearch'; +import { SearchHit } from '../../../../../../../typings/elasticsearch'; import { AgentConfiguration } from '../../../../common/agent_configuration/configuration_types'; // needed for backwards compatability // All settings except `transaction_sample_rate` and `transaction_max_spans` are stored as strings (they are stored as float and integer respectively) export function convertConfigSettingsToString( - hit: ESSearchHit + hit: SearchHit ) { const config = hit._source; diff --git a/x-pack/plugins/apm/server/lib/settings/agent_configuration/create_agent_config_index.ts b/x-pack/plugins/apm/server/lib/settings/agent_configuration/create_agent_config_index.ts index 4f2225e3cec18f..7ec850717dab1c 100644 --- a/x-pack/plugins/apm/server/lib/settings/agent_configuration/create_agent_config_index.ts +++ b/x-pack/plugins/apm/server/lib/settings/agent_configuration/create_agent_config_index.ts @@ -8,7 +8,7 @@ import { ElasticsearchClient, Logger } from 'src/core/server'; import { createOrUpdateIndex, - MappingsDefinition, + Mappings, } from '../../../../../observability/server'; import { APMConfig } from '../../..'; import { getApmIndicesConfig } from '../apm_indices/get_apm_indices'; @@ -31,7 +31,7 @@ export async function createApmAgentConfigurationIndex({ }); } -const mappings: MappingsDefinition = { +const mappings: Mappings = { dynamic: 'strict', dynamic_templates: [ { diff --git a/x-pack/plugins/apm/server/lib/settings/agent_configuration/find_exact_configuration.ts b/x-pack/plugins/apm/server/lib/settings/agent_configuration/find_exact_configuration.ts index 972c076d88e767..9fd4849c7640a8 100644 --- a/x-pack/plugins/apm/server/lib/settings/agent_configuration/find_exact_configuration.ts +++ b/x-pack/plugins/apm/server/lib/settings/agent_configuration/find_exact_configuration.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { ESSearchHit } from '../../../../../../../typings/elasticsearch'; +import { SearchHit } from '../../../../../../../typings/elasticsearch'; import { AgentConfiguration } from '../../../../common/agent_configuration/configuration_types'; import { SERVICE_ENVIRONMENT, @@ -46,9 +46,7 @@ export function findExactConfiguration({ params ); - const hit = resp.hits.hits[0] as - | ESSearchHit - | undefined; + const hit = resp.hits.hits[0] as SearchHit | undefined; if (!hit) { return; diff --git a/x-pack/plugins/apm/server/lib/settings/agent_configuration/search_configurations.ts b/x-pack/plugins/apm/server/lib/settings/agent_configuration/search_configurations.ts index 12ba0939508e3f..7454128a741d5c 100644 --- a/x-pack/plugins/apm/server/lib/settings/agent_configuration/search_configurations.ts +++ b/x-pack/plugins/apm/server/lib/settings/agent_configuration/search_configurations.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { ESSearchHit } from '../../../../../../../typings/elasticsearch'; +import { SearchHit } from '../../../../../../../typings/elasticsearch'; import { SERVICE_NAME, SERVICE_ENVIRONMENT, @@ -75,9 +75,7 @@ export async function searchConfigurations({ params ); - const hit = resp.hits.hits[0] as - | ESSearchHit - | undefined; + const hit = resp.hits.hits[0] as SearchHit | undefined; if (!hit) { return; diff --git a/x-pack/plugins/apm/server/lib/settings/custom_link/create_custom_link_index.ts b/x-pack/plugins/apm/server/lib/settings/custom_link/create_custom_link_index.ts index 037f54344d770d..3965e363499fc7 100644 --- a/x-pack/plugins/apm/server/lib/settings/custom_link/create_custom_link_index.ts +++ b/x-pack/plugins/apm/server/lib/settings/custom_link/create_custom_link_index.ts @@ -6,9 +6,10 @@ */ import { ElasticsearchClient, Logger } from 'src/core/server'; +import { PropertyBase } from '@elastic/elasticsearch/api/types'; import { createOrUpdateIndex, - MappingsDefinition, + Mappings, } from '../../../../../observability/server'; import { APMConfig } from '../../..'; import { getApmIndicesConfig } from '../apm_indices/get_apm_indices'; @@ -31,7 +32,7 @@ export const createApmCustomLinkIndex = async ({ }); }; -const mappings: MappingsDefinition = { +const mappings: Mappings = { dynamic: 'strict', properties: { '@timestamp': { @@ -45,7 +46,8 @@ const mappings: MappingsDefinition = { type: 'keyword', }, }, - }, + // FIXME: PropertyBase type is missing .fields + } as PropertyBase, url: { type: 'keyword', }, diff --git a/x-pack/plugins/apm/server/lib/settings/custom_link/get_transaction.ts b/x-pack/plugins/apm/server/lib/settings/custom_link/get_transaction.ts index 8e343ecfe6a642..d3d9b452853540 100644 --- a/x-pack/plugins/apm/server/lib/settings/custom_link/get_transaction.ts +++ b/x-pack/plugins/apm/server/lib/settings/custom_link/get_transaction.ts @@ -6,6 +6,7 @@ */ import * as t from 'io-ts'; +import { compact } from 'lodash'; import { Setup } from '../../helpers/setup_request'; import { ProcessorEvent } from '../../../../common/processor_event'; import { filterOptionsRt } from './custom_link_types'; @@ -22,15 +23,15 @@ export function getTransaction({ return withApmSpan('get_transaction_for_custom_link', async () => { const { apmEventClient } = setup; - const esFilters = Object.entries(filters) - // loops through the filters splitting the value by comma and removing white spaces - .map(([key, value]) => { - if (value) { - return { terms: { [key]: splitFilterValueByComma(value) } }; - } - }) - // removes filters without value - .filter((value) => value); + const esFilters = compact( + Object.entries(filters) + // loops through the filters splitting the value by comma and removing white spaces + .map(([key, value]) => { + if (value) { + return { terms: { [key]: splitFilterValueByComma(value) } }; + } + }) + ); const params = { terminateAfter: 1, diff --git a/x-pack/plugins/apm/server/lib/settings/custom_link/list_custom_links.ts b/x-pack/plugins/apm/server/lib/settings/custom_link/list_custom_links.ts index 7437b8328b876f..f6b41f462c99ff 100644 --- a/x-pack/plugins/apm/server/lib/settings/custom_link/list_custom_links.ts +++ b/x-pack/plugins/apm/server/lib/settings/custom_link/list_custom_links.ts @@ -6,6 +6,7 @@ */ import * as t from 'io-ts'; +import { QueryContainer } from '@elastic/elasticsearch/api/types'; import { CustomLink, CustomLinkES, @@ -31,7 +32,7 @@ export function listCustomLinks({ should: [ { term: { [key]: value } }, { bool: { must_not: [{ exists: { field: key } }] } }, - ], + ] as QueryContainer[], }, }; }); @@ -48,7 +49,7 @@ export function listCustomLinks({ sort: [ { 'label.keyword': { - order: 'asc', + order: 'asc' as const, }, }, ], diff --git a/x-pack/plugins/apm/server/lib/traces/get_trace_items.ts b/x-pack/plugins/apm/server/lib/traces/get_trace_items.ts index 0b158d9e572850..a946fa66a3b92e 100644 --- a/x-pack/plugins/apm/server/lib/traces/get_trace_items.ts +++ b/x-pack/plugins/apm/server/lib/traces/get_trace_items.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { QueryContainer } from '@elastic/elasticsearch/api/types'; import { ProcessorEvent } from '../../../common/processor_event'; import { TRACE_ID, @@ -75,7 +76,7 @@ export async function getTraceItems( filter: [ { term: { [TRACE_ID]: traceId } }, ...rangeQuery(start, end), - ], + ] as QueryContainer[], should: { exists: { field: PARENT_ID }, }, 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 index 34c2f39ca04c09..a4ff487645a4b5 100644 --- 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 @@ -14,9 +14,11 @@ Array [ "aggs": Object { "transaction_type": Object { "top_metrics": Object { - "metrics": Object { - "field": "transaction.type", - }, + "metrics": Array [ + Object { + "field": "transaction.type", + }, + ], "sort": Object { "@timestamp": "desc", }, @@ -210,9 +212,11 @@ Array [ "aggs": Object { "transaction_type": Object { "top_metrics": Object { - "metrics": Object { - "field": "transaction.type", - }, + "metrics": Array [ + Object { + "field": "transaction.type", + }, + ], "sort": Object { "@timestamp": "desc", }, diff --git a/x-pack/plugins/apm/server/lib/transaction_groups/fetcher.ts b/x-pack/plugins/apm/server/lib/transaction_groups/fetcher.ts index 6308236000a53c..c1bf363b49d1c0 100644 --- a/x-pack/plugins/apm/server/lib/transaction_groups/fetcher.ts +++ b/x-pack/plugins/apm/server/lib/transaction_groups/fetcher.ts @@ -8,6 +8,7 @@ import { sortBy, take } from 'lodash'; import moment from 'moment'; import { Unionize } from 'utility-types'; +import { asMutableArray } from '../../../common/utils/as_mutable_array'; import { AggregationOptionsByType } from '../../../../../../typings/elasticsearch'; import { PromiseReturnType } from '../../../../observability/typings/common'; import { @@ -132,14 +133,14 @@ export function transactionGroupsFetcher( ...(isTopTraces ? { composite: { - sources: [ + sources: asMutableArray([ { [SERVICE_NAME]: { terms: { field: SERVICE_NAME } } }, { [TRANSACTION_NAME]: { terms: { field: TRANSACTION_NAME }, }, }, - ], + ] as const), size, }, } 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 index 5409f919bf8950..86be82faee5782 100644 --- 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 @@ -6,9 +6,9 @@ */ import { merge } from 'lodash'; +import { estypes } from '@elastic/elasticsearch'; import { TRANSACTION_TYPE } from '../../../common/elasticsearch_fieldnames'; import { arrayUnionToCallable } from '../../../common/utils/array_union_to_callable'; -import { AggregationInputMap } from '../../../../../../typings/elasticsearch'; import { TransactionGroupRequestBase, TransactionGroupSetup } from './fetcher'; import { getTransactionDurationFieldForAggregatedTransactions } from '../helpers/aggregated_transactions'; import { withApmSpan } from '../../utils/with_apm_span'; @@ -23,8 +23,8 @@ type BucketKey = string | Record; function mergeRequestWithAggs< TRequestBase extends TransactionGroupRequestBase, - TInputMap extends AggregationInputMap ->(request: TRequestBase, aggs: TInputMap) { + TAggregationMap extends Record +>(request: TRequestBase, aggs: TAggregationMap) { return merge({}, request, { body: { aggs: { @@ -71,11 +71,13 @@ export function getCounts({ request, setup }: MetricParams) { transaction_type: { top_metrics: { sort: { - '@timestamp': 'desc', + '@timestamp': 'desc' as const, }, - metrics: { - field: TRANSACTION_TYPE, - } as const, + metrics: [ + { + field: TRANSACTION_TYPE, + } as const, + ], }, }, }); diff --git a/x-pack/plugins/apm/server/lib/transactions/distribution/get_buckets/index.ts b/x-pack/plugins/apm/server/lib/transactions/distribution/get_buckets/index.ts index fb7544e5fcb8dc..966bf4ab3c58bf 100644 --- a/x-pack/plugins/apm/server/lib/transactions/distribution/get_buckets/index.ts +++ b/x-pack/plugins/apm/server/lib/transactions/distribution/get_buckets/index.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import { QueryContainer } from '@elastic/elasticsearch/api/types'; import { withApmSpan } from '../../../../utils/with_apm_span'; import { SERVICE_NAME, @@ -86,7 +86,7 @@ export async function getBuckets({ ...rangeQuery(start, end), ...environmentQuery(environment), ...kqlQuery(kuery), - ]; + ] as QueryContainer[]; async function getSamplesForDistributionBuckets() { const response = await withApmSpan( @@ -106,7 +106,7 @@ export async function getBuckets({ should: [ { term: { [TRACE_ID]: traceId } }, { term: { [TRANSACTION_ID]: transactionId } }, - ], + ] as QueryContainer[], }, }, aggs: { @@ -122,10 +122,10 @@ export async function getBuckets({ metrics: [ { field: TRANSACTION_ID }, { field: TRACE_ID }, - ] as const, + ], size: 10, sort: { - _score: 'desc', + _score: 'desc' as const, }, }, }, diff --git a/x-pack/plugins/apm/server/lib/transactions/get_anomaly_data/fetcher.ts b/x-pack/plugins/apm/server/lib/transactions/get_anomaly_data/fetcher.ts index a35780539a2567..8b068fd6bd2fb2 100644 --- a/x-pack/plugins/apm/server/lib/transactions/get_anomaly_data/fetcher.ts +++ b/x-pack/plugins/apm/server/lib/transactions/get_anomaly_data/fetcher.ts @@ -5,9 +5,11 @@ * 2.0. */ +import { QueryContainer } from '@elastic/elasticsearch/api/types'; import { ESSearchResponse } from '../../../../../../../typings/elasticsearch'; import { PromiseReturnType } from '../../../../../observability/typings/common'; import { rangeQuery } from '../../../../server/utils/queries'; +import { asMutableArray } from '../../../../common/utils/as_mutable_array'; import { withApmSpan } from '../../../utils/with_apm_span'; import { Setup } from '../../helpers/setup_request'; @@ -42,7 +44,7 @@ export function anomalySeriesFetcher({ { term: { partition_field_value: serviceName } }, { term: { by_field_value: transactionType } }, ...rangeQuery(start, end, 'timestamp'), - ], + ] as QueryContainer[], }, }, aggs: { @@ -60,11 +62,11 @@ export function anomalySeriesFetcher({ aggs: { anomaly_score: { top_metrics: { - metrics: [ + metrics: asMutableArray([ { field: 'record_score' }, { field: 'timestamp' }, { field: 'bucket_span' }, - ] as const, + ] as const), sort: { record_score: 'desc' as const, }, diff --git a/x-pack/plugins/apm/server/lib/transactions/get_transaction/index.ts b/x-pack/plugins/apm/server/lib/transactions/get_transaction/index.ts index a089850e427e64..b4323ae7f51e28 100644 --- a/x-pack/plugins/apm/server/lib/transactions/get_transaction/index.ts +++ b/x-pack/plugins/apm/server/lib/transactions/get_transaction/index.ts @@ -12,6 +12,7 @@ import { import { rangeQuery } from '../../../../server/utils/queries'; import { Setup, SetupTimeRange } from '../../helpers/setup_request'; import { ProcessorEvent } from '../../../../common/processor_event'; +import { asMutableArray } from '../../../../common/utils/as_mutable_array'; import { withApmSpan } from '../../../utils/with_apm_span'; export function getTransaction({ @@ -34,11 +35,11 @@ export function getTransaction({ size: 1, query: { bool: { - filter: [ + filter: asMutableArray([ { term: { [TRANSACTION_ID]: transactionId } }, { term: { [TRACE_ID]: traceId } }, ...rangeQuery(start, end), - ], + ]), }, }, }, diff --git a/x-pack/plugins/apm/server/projections/metrics.ts b/x-pack/plugins/apm/server/projections/metrics.ts index ca43d0a8fb3c84..68056f091c8738 100644 --- a/x-pack/plugins/apm/server/projections/metrics.ts +++ b/x-pack/plugins/apm/server/projections/metrics.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { QueryContainer } from '@elastic/elasticsearch/api/types'; import { Setup, SetupTimeRange } from '../../server/lib/helpers/setup_request'; import { SERVICE_NAME, @@ -51,7 +52,7 @@ export function getMetricsProjection({ ...rangeQuery(start, end), ...environmentQuery(environment), ...kqlQuery(kuery), - ]; + ] as QueryContainer[]; return { apm: { diff --git a/x-pack/plugins/apm/server/projections/rum_page_load_transactions.ts b/x-pack/plugins/apm/server/projections/rum_page_load_transactions.ts index 9c6ea6bc83511d..29fc85128ff3f0 100644 --- a/x-pack/plugins/apm/server/projections/rum_page_load_transactions.ts +++ b/x-pack/plugins/apm/server/projections/rum_page_load_transactions.ts @@ -46,9 +46,7 @@ export function getRumPageLoadTransactionsProjection({ ? [ { wildcard: { - 'url.full': { - value: `*${urlQuery}*`, - }, + 'url.full': `*${urlQuery}*`, }, }, ] @@ -92,9 +90,7 @@ export function getRumErrorsProjection({ ? [ { wildcard: { - 'url.full': { - value: `*${urlQuery}*`, - }, + 'url.full': `*${urlQuery}*`, }, }, ] diff --git a/x-pack/plugins/apm/server/projections/transactions.ts b/x-pack/plugins/apm/server/projections/transactions.ts index 7955518d56f038..dd16b0b910abfe 100644 --- a/x-pack/plugins/apm/server/projections/transactions.ts +++ b/x-pack/plugins/apm/server/projections/transactions.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { QueryContainer } from '@elastic/elasticsearch/api/types'; import { Setup, SetupTimeRange } from '../../server/lib/helpers/setup_request'; import { SERVICE_NAME, @@ -61,7 +62,7 @@ export function getTransactionsProjection({ ...rangeQuery(start, end), ...environmentQuery(environment), ...kqlQuery(kuery), - ], + ] as QueryContainer[], }; return { diff --git a/x-pack/plugins/apm/server/projections/typings.ts b/x-pack/plugins/apm/server/projections/typings.ts index 558f165d43cf5c..bb90aa0bf5eb4b 100644 --- a/x-pack/plugins/apm/server/projections/typings.ts +++ b/x-pack/plugins/apm/server/projections/typings.ts @@ -4,20 +4,19 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - -import { - AggregationOptionsByType, - AggregationInputMap, - ESSearchBody, -} from '../../../../../typings/elasticsearch'; +import { estypes } from '@elastic/elasticsearch'; +import { AggregationOptionsByType } from '../../../../../typings/elasticsearch'; import { APMEventESSearchRequest } from '../lib/helpers/create_es_client/create_apm_event_client'; export type Projection = Omit & { - body: Omit & { + body: Omit< + Required['body'], + 'aggs' | 'aggregations' + > & { aggs?: { [key: string]: { terms: AggregationOptionsByType['terms'] & { field: string }; - aggs?: AggregationInputMap; + aggs?: Record; }; }; }; diff --git a/x-pack/plugins/apm/server/projections/util/merge_projection/index.test.ts b/x-pack/plugins/apm/server/projections/util/merge_projection/index.test.ts index c38859056afeb4..16b2c6f8e60619 100644 --- a/x-pack/plugins/apm/server/projections/util/merge_projection/index.test.ts +++ b/x-pack/plugins/apm/server/projections/util/merge_projection/index.test.ts @@ -13,11 +13,11 @@ describe('mergeProjection', () => { mergeProjection( { apm: { events: [] }, - body: { query: { bool: { must: [{ terms: ['a'] }] } } }, + body: { query: { bool: { must: [{ terms: { field: ['a'] } }] } } }, }, { apm: { events: [] }, - body: { query: { bool: { must: [{ term: 'b' }] } } }, + body: { query: { bool: { must: [{ term: { field: 'b' } }] } } }, } ) ).toEqual({ @@ -29,7 +29,7 @@ describe('mergeProjection', () => { bool: { must: [ { - term: 'b', + term: { field: 'b' }, }, ], }, diff --git a/x-pack/plugins/apm/server/projections/util/merge_projection/index.ts b/x-pack/plugins/apm/server/projections/util/merge_projection/index.ts index 7f087070648626..fb2d981dd4a1f5 100644 --- a/x-pack/plugins/apm/server/projections/util/merge_projection/index.ts +++ b/x-pack/plugins/apm/server/projections/util/merge_projection/index.ts @@ -7,20 +7,12 @@ import { cloneDeep, isPlainObject, mergeWith } from 'lodash'; import { DeepPartial } from 'utility-types'; -import { - AggregationInputMap, - ESSearchBody, -} from '../../../../../../../typings/elasticsearch'; import { APMEventESSearchRequest } from '../../../lib/helpers/create_es_client/create_apm_event_client'; import { Projection } from '../../typings'; type PlainObject = Record; -type SourceProjection = Omit, 'body'> & { - body: Omit, 'aggs'> & { - aggs?: AggregationInputMap; - }; -}; +type SourceProjection = DeepPartial; type DeepMerge = U extends PlainObject ? T extends PlainObject diff --git a/x-pack/plugins/canvas/server/collectors/custom_element_collector.ts b/x-pack/plugins/canvas/server/collectors/custom_element_collector.ts index 17b714f8b72b0f..144d77df064c75 100644 --- a/x-pack/plugins/canvas/server/collectors/custom_element_collector.ts +++ b/x-pack/plugins/canvas/server/collectors/custom_element_collector.ts @@ -5,7 +5,6 @@ * 2.0. */ -import { SearchResponse } from 'elasticsearch'; import { get } from 'lodash'; import { MakeSchemaFrom } from 'src/plugins/usage_collection/server'; import { collectFns } from './collector_helpers'; @@ -125,12 +124,10 @@ const customElementCollector: TelemetryCollector = async function customElementC body: { query: { bool: { filter: { term: { type: CUSTOM_ELEMENT_TYPE } } } } }, }; - const { body: esResponse } = await esClient.search>( - customElementParams - ); + const { body: esResponse } = await esClient.search(customElementParams); if (get(esResponse, 'hits.hits.length') > 0) { - const customElements = esResponse.hits.hits.map((hit) => hit._source[CUSTOM_ELEMENT_TYPE]); + const customElements = esResponse.hits.hits.map((hit) => hit._source![CUSTOM_ELEMENT_TYPE]); return summarizeCustomElements(customElements); } diff --git a/x-pack/plugins/canvas/server/collectors/workpad_collector.ts b/x-pack/plugins/canvas/server/collectors/workpad_collector.ts index 7cb1d17dff4379..7342cb5d40357f 100644 --- a/x-pack/plugins/canvas/server/collectors/workpad_collector.ts +++ b/x-pack/plugins/canvas/server/collectors/workpad_collector.ts @@ -5,7 +5,6 @@ * 2.0. */ -import { SearchResponse } from 'elasticsearch'; import { sum as arraySum, min as arrayMin, max as arrayMax, get } from 'lodash'; import { MakeSchemaFrom } from 'src/plugins/usage_collection/server'; import { CANVAS_TYPE } from '../../common/lib/constants'; @@ -230,7 +229,6 @@ export function summarizeWorkpads(workpadDocs: CanvasWorkpad[]): WorkpadTelemetr variables: variableInfo, }; } -type ESResponse = SearchResponse; const workpadCollector: TelemetryCollector = async function (kibanaIndex, esClient) { const searchParams = { @@ -241,10 +239,10 @@ const workpadCollector: TelemetryCollector = async function (kibanaIndex, esClie body: { query: { bool: { filter: { term: { type: CANVAS_TYPE } } } } }, }; - const { body: esResponse } = await esClient.search(searchParams); + const { body: esResponse } = await esClient.search(searchParams); if (get(esResponse, 'hits.hits.length') > 0) { - const workpads = esResponse.hits.hits.map((hit) => hit._source[CANVAS_TYPE]); + const workpads = esResponse.hits.hits.map((hit) => hit._source![CANVAS_TYPE]); return summarizeWorkpads(workpads); } diff --git a/x-pack/plugins/cases/server/services/alerts/index.ts b/x-pack/plugins/cases/server/services/alerts/index.ts index 6ce4db61ab9563..db8e841f45ee4a 100644 --- a/x-pack/plugins/cases/server/services/alerts/index.ts +++ b/x-pack/plugins/cases/server/services/alerts/index.ts @@ -84,8 +84,9 @@ export class AlertService { return; } - const results = await scopedClusterClient.mget({ body: { docs } }); + const results = await scopedClusterClient.mget({ body: { docs } }); + // @ts-expect-error @elastic/elasticsearch _source is optional return results.body; } catch (error) { throw createCaseError({ diff --git a/x-pack/plugins/data_enhanced/server/collectors/fetch.ts b/x-pack/plugins/data_enhanced/server/collectors/fetch.ts index 428de148fdd4f0..f0a1ebf52f6a1e 100644 --- a/x-pack/plugins/data_enhanced/server/collectors/fetch.ts +++ b/x-pack/plugins/data_enhanced/server/collectors/fetch.ts @@ -36,7 +36,8 @@ export function fetchProvider(config$: Observable, logger: L }, }); - const { buckets } = esResponse.aggregations.persisted; + // @ts-expect-error @elastic/elasticsearch no way to declare a type for aggregations + const buckets: SessionPersistedTermsBucket[] = esResponse.aggregations!.persisted.buckets; if (!buckets.length) { return { transientCount: 0, persistedCount: 0, totalCount: 0 }; } diff --git a/x-pack/plugins/data_enhanced/server/routes/session.ts b/x-pack/plugins/data_enhanced/server/routes/session.ts index 185032bd25bb63..0b786f44454a98 100644 --- a/x-pack/plugins/data_enhanced/server/routes/session.ts +++ b/x-pack/plugins/data_enhanced/server/routes/session.ts @@ -98,7 +98,7 @@ export function registerSessionRoutes(router: DataEnhancedPluginRouter, logger: page: schema.maybe(schema.number()), perPage: schema.maybe(schema.number()), sortField: schema.maybe(schema.string()), - sortOrder: schema.maybe(schema.string()), + sortOrder: schema.maybe(schema.oneOf([schema.literal('desc'), schema.literal('asc')])), filter: schema.maybe(schema.string()), searchFields: schema.maybe(schema.arrayOf(schema.string())), search: schema.maybe(schema.string()), diff --git a/x-pack/plugins/data_enhanced/server/search/eql_search_strategy.ts b/x-pack/plugins/data_enhanced/server/search/eql_search_strategy.ts index 526d01dade7a74..9c1bedc4d5f1cf 100644 --- a/x-pack/plugins/data_enhanced/server/search/eql_search_strategy.ts +++ b/x-pack/plugins/data_enhanced/server/search/eql_search_strategy.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { ApiResponse } from '@elastic/elasticsearch'; import { tap } from 'rxjs/operators'; import type { IScopedClusterClient, Logger } from 'kibana/server'; import type { ISearchStrategy } from '../../../../../src/plugins/data/server'; @@ -51,13 +51,10 @@ export const eqlSearchStrategyProvider = ( ...request.params, }; const promise = id - ? client.get({ ...params, id }, request.options) - : client.search( - params as EqlSearchStrategyRequest['params'], - request.options - ); + ? client.get({ ...params, id }, request.options) + : client.search(params as EqlSearchStrategyRequest['params'], request.options); const response = await shimAbortSignal(promise, options.abortSignal); - return toEqlKibanaSearchResponse(response); + return toEqlKibanaSearchResponse(response as ApiResponse); }; const cancel = async () => { diff --git a/x-pack/plugins/data_enhanced/server/search/es_search_strategy.test.ts b/x-pack/plugins/data_enhanced/server/search/es_search_strategy.test.ts index d529e981aaea10..2ae79f4e144e01 100644 --- a/x-pack/plugins/data_enhanced/server/search/es_search_strategy.test.ts +++ b/x-pack/plugins/data_enhanced/server/search/es_search_strategy.test.ts @@ -347,7 +347,7 @@ describe('ES search strategy', () => { expect(mockGetCaller).toBeCalled(); const request = mockGetCaller.mock.calls[0][0]; - expect(request).toEqual({ id, keep_alive: keepAlive }); + expect(request).toEqual({ id, body: { keep_alive: keepAlive } }); }); it('throws normalized error on ElasticsearchClientError', async () => { diff --git a/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts b/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts index fc1cc63146358e..aec2e7bd533ec5 100644 --- a/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts +++ b/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts @@ -34,7 +34,6 @@ import { getIgnoreThrottled, } from './request_utils'; import { toAsyncKibanaSearchResponse } from './response_utils'; -import { AsyncSearchResponse } from './types'; import { ConfigSchema } from '../../config'; import { getKbnServerError, KbnServerError } from '../../../../../src/plugins/kibana_utils/server'; @@ -66,12 +65,14 @@ export const enhancedEsSearchStrategyProvider = ( ...(await getDefaultAsyncSubmitParams(uiSettingsClient, config, options)), ...request.params, }; - const promise = id - ? client.get({ ...params, id }) - : client.submit(params); + const promise = id ? client.get({ ...params, id }) : client.submit(params); const { body } = await shimAbortSignal(promise, options.abortSignal); const response = shimHitsTotal(body.response, options); - return toAsyncKibanaSearchResponse({ ...body, response }); + + return toAsyncKibanaSearchResponse( + // @ts-expect-error @elastic/elasticsearch start_time_in_millis expected to be number + { ...body, response } + ); }; const cancel = async () => { @@ -167,7 +168,7 @@ export const enhancedEsSearchStrategyProvider = ( extend: async (id, keepAlive, options, { esClient }) => { logger.debug(`extend ${id} by ${keepAlive}`); try { - await esClient.asCurrentUser.asyncSearch.get({ id, keep_alive: keepAlive }); + await esClient.asCurrentUser.asyncSearch.get({ id, body: { keep_alive: keepAlive } }); } catch (e) { throw getKbnServerError(e); } diff --git a/x-pack/plugins/data_enhanced/server/search/session/get_search_status.ts b/x-pack/plugins/data_enhanced/server/search/session/get_search_status.ts index 9fe4c293a87413..dffccbee9db924 100644 --- a/x-pack/plugins/data_enhanced/server/search/session/get_search_status.ts +++ b/x-pack/plugins/data_enhanced/server/search/session/get_search_status.ts @@ -18,6 +18,7 @@ export async function getSearchStatus( ): Promise> { // TODO: Handle strategies other than the default one try { + // @ts-expect-error @elastic/elasticsearch status method is not defined const apiResponse: ApiResponse = await client.asyncSearch.status({ id: asyncId, }); diff --git a/x-pack/plugins/data_enhanced/server/search/types.ts b/x-pack/plugins/data_enhanced/server/search/types.ts index df3ca86466aa98..e2a4e2ce74f151 100644 --- a/x-pack/plugins/data_enhanced/server/search/types.ts +++ b/x-pack/plugins/data_enhanced/server/search/types.ts @@ -5,11 +5,12 @@ * 2.0. */ +import type { estypes } from '@elastic/elasticsearch'; import { SearchResponse, ShardsResponse } from 'elasticsearch'; export interface AsyncSearchResponse { id?: string; - response: SearchResponse; + response: estypes.SearchResponse; start_time_in_millis: number; expiration_time_in_millis: number; is_partial: boolean; diff --git a/x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts b/x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts index 73bf17195a5db5..efe3186f978055 100644 --- a/x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts +++ b/x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts @@ -327,7 +327,15 @@ describe('queryEventsBySavedObject', () => { asApiResponse({ hits: { hits: [], - total: { value: 0 }, + total: { relation: 'eq', value: 0 }, + }, + took: 0, + timed_out: false, + _shards: { + failed: 0, + successful: 0, + total: 0, + skipped: 0, }, }) ); @@ -391,11 +399,13 @@ describe('queryEventsBySavedObject', () => { }, }, "size": 10, - "sort": Object { - "@timestamp": Object { - "order": "asc", + "sort": Array [ + Object { + "@timestamp": Object { + "order": "asc", + }, }, - }, + ], }, "index": "index-name", "track_total_hits": true, @@ -408,7 +418,15 @@ describe('queryEventsBySavedObject', () => { asApiResponse({ hits: { hits: [], - total: { value: 0 }, + total: { relation: 'eq', value: 0 }, + }, + took: 0, + timed_out: false, + _shards: { + failed: 0, + successful: 0, + total: 0, + skipped: 0, }, }) ); @@ -474,11 +492,13 @@ describe('queryEventsBySavedObject', () => { }, }, "size": 10, - "sort": Object { - "@timestamp": Object { - "order": "asc", + "sort": Array [ + Object { + "@timestamp": Object { + "order": "asc", + }, }, - }, + ], }, "index": "index-name", "track_total_hits": true, @@ -491,7 +511,15 @@ describe('queryEventsBySavedObject', () => { asApiResponse({ hits: { hits: [], - total: { value: 0 }, + total: { relation: 'eq', value: 0 }, + }, + took: 0, + timed_out: false, + _shards: { + failed: 0, + successful: 0, + total: 0, + skipped: 0, }, }) ); @@ -507,7 +535,7 @@ describe('queryEventsBySavedObject', () => { expect(query).toMatchObject({ index: 'index-name', body: { - sort: { 'event.end': { order: 'desc' } }, + sort: [{ 'event.end': { order: 'desc' } }], }, }); }); @@ -517,7 +545,15 @@ describe('queryEventsBySavedObject', () => { asApiResponse({ hits: { hits: [], - total: { value: 0 }, + total: { relation: 'eq', value: 0 }, + }, + took: 0, + timed_out: false, + _shards: { + failed: 0, + successful: 0, + total: 0, + skipped: 0, }, }) ); @@ -591,11 +627,13 @@ describe('queryEventsBySavedObject', () => { }, }, "size": 10, - "sort": Object { - "@timestamp": Object { - "order": "asc", + "sort": Array [ + Object { + "@timestamp": Object { + "order": "asc", + }, }, - }, + ], }, "index": "index-name", "track_total_hits": true, @@ -608,7 +646,15 @@ describe('queryEventsBySavedObject', () => { asApiResponse({ hits: { hits: [], - total: { value: 0 }, + total: { relation: 'eq', value: 0 }, + }, + took: 0, + timed_out: false, + _shards: { + failed: 0, + successful: 0, + total: 0, + skipped: 0, }, }) ); @@ -690,11 +736,13 @@ describe('queryEventsBySavedObject', () => { }, }, "size": 10, - "sort": Object { - "@timestamp": Object { - "order": "asc", + "sort": Array [ + Object { + "@timestamp": Object { + "order": "asc", + }, }, - }, + ], }, "index": "index-name", "track_total_hits": true, diff --git a/x-pack/plugins/event_log/server/es/cluster_client_adapter.ts b/x-pack/plugins/event_log/server/es/cluster_client_adapter.ts index f025801a459553..5d7be2278d55dc 100644 --- a/x-pack/plugins/event_log/server/es/cluster_client_adapter.ts +++ b/x-pack/plugins/event_log/server/es/cluster_client_adapter.ts @@ -7,9 +7,10 @@ import { Subject } from 'rxjs'; import { bufferTime, filter as rxFilter, switchMap } from 'rxjs/operators'; -import { reject, isUndefined } from 'lodash'; +import { reject, isUndefined, isNumber } from 'lodash'; import type { PublicMethodsOf } from '@kbn/utility-types'; import { Logger, ElasticsearchClient } from 'src/core/server'; +import { estypes } from '@elastic/elasticsearch'; import { EsContext } from '.'; import { IEvent, IValidatedEvent, SAVED_OBJECT_REL_PRIMARY } from '../types'; import { FindOptionsType } from '../event_log_client'; @@ -135,14 +136,13 @@ export class ClusterClientAdapter { } public async doesIndexTemplateExist(name: string): Promise { - let result; try { const esClient = await this.elasticsearchClientPromise; - result = (await esClient.indices.existsTemplate({ name })).body; + const { body } = await esClient.indices.existsTemplate({ name }); + return body as boolean; } catch (err) { throw new Error(`error checking existance of index template: ${err.message}`); } - return result as boolean; } public async createIndexTemplate(name: string, template: Record): Promise { @@ -162,20 +162,16 @@ export class ClusterClientAdapter { } public async doesAliasExist(name: string): Promise { - let result; try { const esClient = await this.elasticsearchClientPromise; - result = (await esClient.indices.existsAlias({ name })).body; + const { body } = await esClient.indices.existsAlias({ name }); + return body as boolean; } catch (err) { throw new Error(`error checking existance of initial index: ${err.message}`); } - return result as boolean; } - public async createIndex( - name: string, - body: string | Record = {} - ): Promise { + public async createIndex(name: string, body: Record = {}): Promise { try { const esClient = await this.elasticsearchClientPromise; await esClient.indices.create({ @@ -228,64 +224,67 @@ export class ClusterClientAdapter { }); throw err; } - const body = { - size: perPage, - from: (page - 1) * perPage, - sort: { [sort_field]: { order: sort_order } }, - query: { - bool: { - filter: dslFilterQuery, - must: reject( - [ - { - nested: { - path: 'kibana.saved_objects', - query: { - bool: { - must: [ - { - term: { - 'kibana.saved_objects.rel': { - value: SAVED_OBJECT_REL_PRIMARY, - }, - }, - }, - { - term: { - 'kibana.saved_objects.type': { - value: type, - }, - }, - }, - { - terms: { - // default maximum of 65,536 terms, configurable by index.max_terms_count - 'kibana.saved_objects.id': ids, - }, - }, - namespaceQuery, - ], + const musts: estypes.QueryContainer[] = [ + { + nested: { + path: 'kibana.saved_objects', + query: { + bool: { + must: [ + { + term: { + 'kibana.saved_objects.rel': { + value: SAVED_OBJECT_REL_PRIMARY, }, }, }, - }, - start && { - range: { - '@timestamp': { - gte: start, + { + term: { + 'kibana.saved_objects.type': { + value: type, + }, }, }, - }, - end && { - range: { - '@timestamp': { - lte: end, + { + terms: { + // default maximum of 65,536 terms, configurable by index.max_terms_count + 'kibana.saved_objects.id': ids, }, }, - }, - ], - isUndefined - ), + namespaceQuery, + ], + }, + }, + }, + }, + ]; + if (start) { + musts.push({ + range: { + '@timestamp': { + gte: start, + }, + }, + }); + } + if (end) { + musts.push({ + range: { + '@timestamp': { + lte: end, + }, + }, + }); + } + + const body: estypes.SearchRequest['body'] = { + size: perPage, + from: (page - 1) * perPage, + sort: [{ [sort_field]: { order: sort_order } }], + query: { + bool: { + filter: dslFilterQuery, + must: reject(musts, isUndefined), }, }, }; @@ -295,7 +294,7 @@ export class ClusterClientAdapter { body: { hits: { hits, total }, }, - } = await esClient.search({ + } = await esClient.search({ index, track_total_hits: true, body, @@ -303,8 +302,8 @@ export class ClusterClientAdapter { return { page, per_page: perPage, - total: total.value, - data: hits.map((hit: { _source: unknown }) => hit._source) as IValidatedEvent[], + total: isNumber(total) ? total : total.value, + data: hits.map((hit) => hit._source), }; } catch (err) { throw new Error( diff --git a/x-pack/plugins/file_upload/server/analyze_file.tsx b/x-pack/plugins/file_upload/server/analyze_file.tsx index 394573eb0cca5a..22396970834928 100644 --- a/x-pack/plugins/file_upload/server/analyze_file.tsx +++ b/x-pack/plugins/file_upload/server/analyze_file.tsx @@ -6,13 +6,7 @@ */ import { IScopedClusterClient } from 'kibana/server'; -import { - AnalysisResult, - FormattedOverrides, - InputData, - InputOverrides, - FindFileStructureResponse, -} from '../common'; +import { AnalysisResult, FormattedOverrides, InputData, InputOverrides } from '../common'; export async function analyzeFile( client: IScopedClusterClient, @@ -20,9 +14,7 @@ export async function analyzeFile( overrides: InputOverrides ): Promise { overrides.explain = overrides.explain === undefined ? 'true' : overrides.explain; - const { - body, - } = await client.asInternalUser.textStructure.findStructure({ + const { body } = await client.asInternalUser.textStructure.findStructure({ body: data, ...overrides, }); @@ -31,6 +23,7 @@ export async function analyzeFile( return { ...(hasOverrides && { overrides: reducedOverrides }), + // @ts-expect-error type incompatible with FindFileStructureResponse results: body, }; } diff --git a/x-pack/plugins/fleet/server/routes/data_streams/handlers.ts b/x-pack/plugins/fleet/server/routes/data_streams/handlers.ts index a928c80f6dd81a..c684c050036124 100644 --- a/x-pack/plugins/fleet/server/routes/data_streams/handlers.ts +++ b/x-pack/plugins/fleet/server/routes/data_streams/handlers.ts @@ -96,6 +96,7 @@ export const getListHandler: RequestHandler = async (context, request, response) // Query backing indices to extract data stream dataset, namespace, and type values const { body: { + // @ts-expect-error @elastic/elasticsearch aggregations are not typed aggregations: { dataset, namespace, type }, }, } = await esClient.search({ diff --git a/x-pack/plugins/fleet/server/services/agents/actions.ts b/x-pack/plugins/fleet/server/services/agents/actions.ts index 3be69893ab252d..bcece7283270b8 100644 --- a/x-pack/plugins/fleet/server/services/agents/actions.ts +++ b/x-pack/plugins/fleet/server/services/agents/actions.ts @@ -359,7 +359,7 @@ export async function getLatestConfigChangeAction( search: policyId, searchFields: ['policy_id'], sortField: 'created_at', - sortOrder: 'DESC', + sortOrder: 'desc', }); if (res.saved_objects[0]) { diff --git a/x-pack/plugins/fleet/server/services/agents/crud.ts b/x-pack/plugins/fleet/server/services/agents/crud.ts index 9aa7bbc9f2b18b..b89b2b6d351b8b 100644 --- a/x-pack/plugins/fleet/server/services/agents/crud.ts +++ b/x-pack/plugins/fleet/server/services/agents/crud.ts @@ -6,11 +6,11 @@ */ import Boom from '@hapi/boom'; -import type { SearchResponse, MGetResponse, GetResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import type { SavedObjectsClientContract, ElasticsearchClient } from 'src/core/server'; import type { AgentSOAttributes, Agent, BulkActionResult, ListWithKuery } from '../../types'; -import type { ESSearchResponse } from '../../../../../../typings/elasticsearch'; + import { appContextService, agentPolicyService } from '../../services'; import type { FleetServerAgent } from '../../../common'; import { isAgentUpgradeable, SO_SEARCH_LIMIT } from '../../../common'; @@ -120,7 +120,7 @@ export async function getAgentsByKuery( const kueryNode = _joinFilters(filters); const body = kueryNode ? { query: esKuery.toElasticsearchQuery(kueryNode) } : {}; - const res = await esClient.search>({ + const res = await esClient.search({ index: AGENTS_INDEX, from: (page - 1) * perPage, size: perPage, @@ -140,7 +140,7 @@ export async function getAgentsByKuery( return { agents, - total: res.body.hits.total.value, + total: (res.body.hits.total as estypes.TotalHits).value, page, perPage, }; @@ -183,13 +183,14 @@ export async function countInactiveAgents( track_total_hits: true, body, }); + // @ts-expect-error value is number | TotalHits return res.body.hits.total.value; } export async function getAgentById(esClient: ElasticsearchClient, agentId: string) { const agentNotFoundError = new AgentNotFoundError(`Agent ${agentId} not found`); try { - const agentHit = await esClient.get({ + const agentHit = await esClient.get({ index: AGENTS_INDEX, id: agentId, }); @@ -210,16 +211,16 @@ export async function getAgentById(esClient: ElasticsearchClient, agentId: strin export function isAgentDocument( maybeDocument: any -): maybeDocument is GetResponse { +): maybeDocument is estypes.MultiGetHit { return '_id' in maybeDocument && '_source' in maybeDocument; } -export type ESAgentDocumentResult = GetResponse; +export type ESAgentDocumentResult = estypes.MultiGetHit; export async function getAgentDocuments( esClient: ElasticsearchClient, agentIds: string[] ): Promise { - const res = await esClient.mget>({ + const res = await esClient.mget({ index: AGENTS_INDEX, body: { docs: agentIds.map((_id) => ({ _id })) }, }); @@ -247,7 +248,7 @@ export async function getAgentByAccessAPIKeyId( esClient: ElasticsearchClient, accessAPIKeyId: string ): Promise { - const res = await esClient.search>({ + const res = await esClient.search({ index: AGENTS_INDEX, q: `access_api_key_id:${escapeSearchQueryPhrase(accessAPIKeyId)}`, }); @@ -309,10 +310,11 @@ export async function bulkUpdateAgents( }); return { - items: res.body.items.map((item: { update: { _id: string; error?: Error } }) => ({ - id: item.update._id, - success: !item.update.error, - error: item.update.error, + items: res.body.items.map((item: estypes.BulkResponseItemContainer) => ({ + id: item.update!._id as string, + success: !item.update!.error, + // @ts-expect-error ErrorCause is not assignable to Error + error: item.update!.error as Error, })), }; } diff --git a/x-pack/plugins/fleet/server/services/agents/helpers.ts b/x-pack/plugins/fleet/server/services/agents/helpers.ts index 89f37a01a6b002..c003d3d546e832 100644 --- a/x-pack/plugins/fleet/server/services/agents/helpers.ts +++ b/x-pack/plugins/fleet/server/services/agents/helpers.ts @@ -5,25 +5,26 @@ * 2.0. */ -import type { GetResponse, SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; -import type { ESSearchHit } from '../../../../../../typings/elasticsearch'; +import type { SearchHit } from '../../../../../../typings/elasticsearch'; import type { Agent, AgentSOAttributes, FleetServerAgent } from '../../types'; type FleetServerAgentESResponse = - | GetResponse - | ESSearchHit - | SearchResponse['hits']['hits'][0]; + | estypes.MultiGetHit + | estypes.SearchResponse['hits']['hits'][0] + | SearchHit; export function searchHitToAgent(hit: FleetServerAgentESResponse): Agent { + // @ts-expect-error @elastic/elasticsearch MultiGetHit._source is optional return { id: hit._id, ...hit._source, - policy_revision: hit._source.policy_revision_idx, + policy_revision: hit._source?.policy_revision_idx, current_error_events: [], access_api_key: undefined, status: undefined, - packages: hit._source.packages ?? [], + packages: hit._source?.packages ?? [], }; } diff --git a/x-pack/plugins/fleet/server/services/agents/reassign.test.ts b/x-pack/plugins/fleet/server/services/agents/reassign.test.ts index 987f4615872335..f040ba57c38be6 100644 --- a/x-pack/plugins/fleet/server/services/agents/reassign.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/reassign.test.ts @@ -47,7 +47,6 @@ describe('reassignAgent (singular)', () => { expect(esClient.update).toBeCalledTimes(1); const calledWith = esClient.update.mock.calls[0]; expect(calledWith[0]?.id).toBe(agentInUnmanagedDoc._id); - // @ts-expect-error expect(calledWith[0]?.body?.doc).toHaveProperty('policy_id', unmanagedAgentPolicySO.id); }); @@ -91,7 +90,6 @@ describe('reassignAgents (plural)', () => { // calls ES update with correct values const calledWith = esClient.bulk.mock.calls[0][0]; // only 1 are unmanaged and bulk write two line per update - // @ts-expect-error expect(calledWith.body.length).toBe(2); // @ts-expect-error expect(calledWith.body[0].update._id).toEqual(agentInUnmanagedDoc._id); @@ -150,8 +148,8 @@ function createClientsMock() { throw new Error(`${id} not found`); } }); - // @ts-expect-error esClientMock.bulk.mockResolvedValue({ + // @ts-expect-error not full interface body: { items: [] }, }); diff --git a/x-pack/plugins/fleet/server/services/agents/status.test.ts b/x-pack/plugins/fleet/server/services/agents/status.test.ts index b11ea7ae7f87ca..35300dfc027696 100644 --- a/x-pack/plugins/fleet/server/services/agents/status.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/status.test.ts @@ -12,8 +12,8 @@ import { getAgentStatusById } from './status'; describe('Agent status service', () => { it('should return inactive when agent is not active', async () => { const mockElasticsearchClient = elasticsearchServiceMock.createClusterClient().asInternalUser; - // @ts-expect-error mockElasticsearchClient.get.mockResolvedValue({ + // @ts-expect-error not full interface body: { _id: 'id', _source: { @@ -29,8 +29,8 @@ describe('Agent status service', () => { it('should return online when agent is active', async () => { const mockElasticsearchClient = elasticsearchServiceMock.createClusterClient().asInternalUser; - // @ts-expect-error mockElasticsearchClient.get.mockResolvedValue({ + // @ts-expect-error not full interface body: { _id: 'id', _source: { @@ -47,8 +47,8 @@ describe('Agent status service', () => { it('should return enrolling when agent is active but never checkin', async () => { const mockElasticsearchClient = elasticsearchServiceMock.createClusterClient().asInternalUser; - // @ts-expect-error mockElasticsearchClient.get.mockResolvedValue({ + // @ts-expect-error not full interface body: { _id: 'id', _source: { @@ -64,8 +64,8 @@ describe('Agent status service', () => { it('should return unenrolling when agent is unenrolling', async () => { const mockElasticsearchClient = elasticsearchServiceMock.createClusterClient().asInternalUser; - // @ts-expect-error mockElasticsearchClient.get.mockResolvedValue({ + // @ts-expect-error not full interface body: { _id: 'id', diff --git a/x-pack/plugins/fleet/server/services/agents/unenroll.test.ts b/x-pack/plugins/fleet/server/services/agents/unenroll.test.ts index 23ba8ac7bbd7ff..3d0692c242096e 100644 --- a/x-pack/plugins/fleet/server/services/agents/unenroll.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/unenroll.test.ts @@ -65,10 +65,8 @@ describe('unenrollAgents (plural)', () => { // calls ES update with correct values const calledWith = esClient.bulk.mock.calls[1][0]; const ids = calledWith?.body - // @ts-expect-error .filter((i: any) => i.update !== undefined) .map((i: any) => i.update._id); - // @ts-expect-error const docs = calledWith?.body.filter((i: any) => i.doc).map((i: any) => i.doc); expect(ids).toHaveLength(2); expect(ids).toEqual(idsToUnenroll); @@ -90,10 +88,8 @@ describe('unenrollAgents (plural)', () => { const onlyUnmanaged = [agentInUnmanagedDoc._id, agentInUnmanagedDoc2._id]; const calledWith = esClient.bulk.mock.calls[1][0]; const ids = calledWith?.body - // @ts-expect-error .filter((i: any) => i.update !== undefined) .map((i: any) => i.update._id); - // @ts-expect-error const docs = calledWith?.body.filter((i: any) => i.doc).map((i: any) => i.doc); expect(ids).toHaveLength(onlyUnmanaged.length); expect(ids).toEqual(onlyUnmanaged); @@ -149,8 +145,8 @@ function createClientMock() { throw new Error('not found'); } }); - // @ts-expect-error esClientMock.bulk.mockResolvedValue({ + // @ts-expect-error not full interface body: { items: [] }, }); diff --git a/x-pack/plugins/fleet/server/services/api_keys/enrollment_api_key.ts b/x-pack/plugins/fleet/server/services/api_keys/enrollment_api_key.ts index 4365c3913f4332..b3edb20d51c4f6 100644 --- a/x-pack/plugins/fleet/server/services/api_keys/enrollment_api_key.ts +++ b/x-pack/plugins/fleet/server/services/api_keys/enrollment_api_key.ts @@ -8,7 +8,6 @@ import uuid from 'uuid'; import Boom from '@hapi/boom'; import { i18n } from '@kbn/i18n'; -import type { GetResponse } from 'elasticsearch'; import { ResponseError } from '@elastic/elasticsearch/lib/errors'; import type { SavedObjectsClientContract, ElasticsearchClient } from 'src/core/server'; @@ -42,10 +41,12 @@ export async function listEnrollmentApiKeys( q: kuery, }); + // @ts-expect-error @elastic/elasticsearch const items = res.body.hits.hits.map(esDocToEnrollmentApiKey); return { items, + // @ts-expect-error value is number | TotalHits total: res.body.hits.total.value, page, perPage, @@ -57,11 +58,12 @@ export async function getEnrollmentAPIKey( id: string ): Promise { try { - const res = await esClient.get>({ + const res = await esClient.get({ index: ENROLLMENT_API_KEYS_INDEX, id, }); + // @ts-expect-error esDocToEnrollmentApiKey doesn't accept optional _source return esDocToEnrollmentApiKey(res.body); } catch (e) { if (e instanceof ResponseError && e.statusCode === 404) { @@ -226,11 +228,12 @@ export async function generateEnrollmentAPIKey( } export async function getEnrollmentAPIKeyById(esClient: ElasticsearchClient, apiKeyId: string) { - const res = await esClient.search>({ + const res = await esClient.search({ index: ENROLLMENT_API_KEYS_INDEX, q: `api_key_id:${escapeSearchQueryPhrase(apiKeyId)}`, }); + // @ts-expect-error esDocToEnrollmentApiKey doesn't accept optional _source const [enrollmentAPIKey] = res.body.hits.hits.map(esDocToEnrollmentApiKey); if (enrollmentAPIKey?.api_key_id !== apiKeyId) { diff --git a/x-pack/plugins/fleet/server/services/artifacts/artifacts.ts b/x-pack/plugins/fleet/server/services/artifacts/artifacts.ts index 9a12f6a3c0bdfc..77785aeb026c12 100644 --- a/x-pack/plugins/fleet/server/services/artifacts/artifacts.ts +++ b/x-pack/plugins/fleet/server/services/artifacts/artifacts.ts @@ -16,7 +16,6 @@ import type { ElasticsearchClient } from 'kibana/server'; import type { ListResult } from '../../../common'; import { FLEET_SERVER_ARTIFACTS_INDEX } from '../../../common'; -import type { ESSearchHit, ESSearchResponse } from '../../../../../../typings/elasticsearch'; import { ArtifactsElasticsearchError } from '../../errors'; @@ -38,11 +37,12 @@ export const getArtifact = async ( id: string ): Promise => { try { - const esData = await esClient.get>({ + const esData = await esClient.get({ index: FLEET_SERVER_ARTIFACTS_INDEX, id, }); + // @ts-expect-error @elastic/elasticsearch _source is optional return esSearchHitToArtifact(esData.body); } catch (e) { if (isElasticsearchItemNotFoundError(e)) { @@ -92,9 +92,7 @@ export const listArtifacts = async ( const { perPage = 20, page = 1, kuery = '', sortField = 'created', sortOrder = 'asc' } = options; try { - const searchResult = await esClient.search< - ESSearchResponse - >({ + const searchResult = await esClient.search({ index: FLEET_SERVER_ARTIFACTS_INDEX, sort: `${sortField}:${sortOrder}`, q: kuery, @@ -103,9 +101,11 @@ export const listArtifacts = async ( }); return { + // @ts-expect-error @elastic/elasticsearch _source is optional items: searchResult.body.hits.hits.map((hit) => esSearchHitToArtifact(hit)), page, perPage, + // @ts-expect-error doesn't handle total as number total: searchResult.body.hits.total.value, }; } catch (e) { diff --git a/x-pack/plugins/fleet/server/services/artifacts/mappings.ts b/x-pack/plugins/fleet/server/services/artifacts/mappings.ts index 43aa111f2efcf6..3b81e47577ff7f 100644 --- a/x-pack/plugins/fleet/server/services/artifacts/mappings.ts +++ b/x-pack/plugins/fleet/server/services/artifacts/mappings.ts @@ -5,13 +5,13 @@ * 2.0. */ -import type { ESSearchHit } from '../../../../../../typings/elasticsearch'; +import type { SearchHit } from '../../../../../../typings/elasticsearch'; import type { Artifact, ArtifactElasticsearchProperties, NewArtifact } from './types'; import { ARTIFACT_DOWNLOAD_RELATIVE_PATH } from './constants'; export const esSearchHitToArtifact = < - T extends Pick, '_id' | '_source'> + T extends Pick, '_id' | '_source'> >({ _id: id, _source: { diff --git a/x-pack/plugins/fleet/server/services/artifacts/mocks.ts b/x-pack/plugins/fleet/server/services/artifacts/mocks.ts index 5569e4ac77d20f..1a10f93f678b3f 100644 --- a/x-pack/plugins/fleet/server/services/artifacts/mocks.ts +++ b/x-pack/plugins/fleet/server/services/artifacts/mocks.ts @@ -10,7 +10,7 @@ import type { ApiResponse } from '@elastic/elasticsearch'; import { ResponseError } from '@elastic/elasticsearch/lib/errors'; import { elasticsearchServiceMock } from '../../../../../../src/core/server/mocks'; -import type { ESSearchHit, ESSearchResponse } from '../../../../../../typings/elasticsearch'; +import type { SearchHit, ESSearchResponse } from '../../../../../../typings/elasticsearch'; import type { Artifact, ArtifactElasticsearchProperties, ArtifactsClientInterface } from './types'; import { newArtifactToElasticsearchProperties } from './mappings'; @@ -77,7 +77,7 @@ export const generateEsRequestErrorApiResponseMock = ( ); }; -export const generateArtifactEsGetSingleHitMock = (): ESSearchHit => { +export const generateArtifactEsGetSingleHitMock = (): SearchHit => { const { id, created, ...newArtifact } = generateArtifactMock(); const _source = { ...newArtifactToElasticsearchProperties(newArtifact), diff --git a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/template.ts b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/template.ts index 456ed95a8c3e45..0b95f8d76627a9 100644 --- a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/template.ts +++ b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/template.ts @@ -497,6 +497,7 @@ const updateExistingDataStream = async ({ await esClient.indices.putMapping({ index: dataStreamName, body: mappings, + // @ts-expect-error @elastic/elasticsearch doesn't declare it on PutMappingRequest write_index_only: true, }); // if update fails, rollover data stream diff --git a/x-pack/plugins/fleet/server/services/epm/elasticsearch/transform/install.ts b/x-pack/plugins/fleet/server/services/epm/elasticsearch/transform/install.ts index cbd09b8d1e7a8b..7d62c0ef41c8db 100644 --- a/x-pack/plugins/fleet/server/services/epm/elasticsearch/transform/install.ts +++ b/x-pack/plugins/fleet/server/services/epm/elasticsearch/transform/install.ts @@ -123,6 +123,7 @@ async function handleTransformInstall({ await esClient.transform.putTransform({ transform_id: transform.installationName, defer_validation: true, + // @ts-expect-error expect object, but given a string body: transform.content, }); } catch (err) { diff --git a/x-pack/plugins/fleet/server/services/epm/elasticsearch/transform/remove.ts b/x-pack/plugins/fleet/server/services/epm/elasticsearch/transform/remove.ts index 248c03e43add9a..39681401ac9555 100644 --- a/x-pack/plugins/fleet/server/services/epm/elasticsearch/transform/remove.ts +++ b/x-pack/plugins/fleet/server/services/epm/elasticsearch/transform/remove.ts @@ -55,6 +55,7 @@ export const deleteTransforms = async (esClient: ElasticsearchClient, transformI await esClient.transport.request( { method: 'DELETE', + // @ts-expect-error @elastic/elasticsearch Transform is empty interface path: `/${transform?.dest?.index}`, }, { diff --git a/x-pack/plugins/fleet/server/services/fleet_server/saved_object_migrations.ts b/x-pack/plugins/fleet/server/services/fleet_server/saved_object_migrations.ts index 1d5a788c3a2c20..f078b214e4dfdb 100644 --- a/x-pack/plugins/fleet/server/services/fleet_server/saved_object_migrations.ts +++ b/x-pack/plugins/fleet/server/services/fleet_server/saved_object_migrations.ts @@ -169,6 +169,7 @@ async function migrateAgentPolicies() { track_total_hits: true, }); + // @ts-expect-error value is number | TotalHits if (res.body.hits.total.value === 0) { return agentPolicyService.createFleetPolicyChangeFleetServer( soClient, diff --git a/x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_add_policy_route.ts b/x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_add_policy_route.ts index a18459d5d21b94..77f14decc56429 100644 --- a/x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_add_policy_route.ts +++ b/x-pack/plugins/index_lifecycle_management/server/routes/api/index/register_add_policy_route.ts @@ -24,6 +24,7 @@ async function addLifecyclePolicy( }, }; + // @ts-expect-error @elastic/elasticsearch UpdateIndexSettingsRequest does not support index property return client.indices.putSettings({ index: indexName, body }); } diff --git a/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_delete_route.ts b/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_delete_route.ts index 72768a1540adce..069adc139a86d7 100644 --- a/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_delete_route.ts +++ b/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_delete_route.ts @@ -17,6 +17,7 @@ async function deletePolicies(client: ElasticsearchClient, policyName: string): ignore: [404], }; + // @ts-expect-error @elastic/elasticsearch DeleteSnapshotLifecycleRequest.policy_id is required return client.ilm.deleteLifecycle({ policy: policyName }, options); } diff --git a/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_fetch_route.ts b/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_fetch_route.ts index 75320b6f2d242a..9aa5e3c24d010b 100644 --- a/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_fetch_route.ts +++ b/x-pack/plugins/index_lifecycle_management/server/routes/api/policies/register_fetch_route.ts @@ -33,6 +33,7 @@ async function fetchPolicies(client: ElasticsearchClient): Promise( + const response = await client.indices.getIndexTemplate( { name: templateName, }, options ); - const { index_templates: templates } = response.body; - return templates?.find((template) => template.name === templateName)?.index_template; + const { index_templates: templates } = response.body as { + index_templates: TemplateFromEs[]; + }; + return templates.find((template) => template.name === templateName)?.index_template; } async function updateIndexTemplate( diff --git a/x-pack/plugins/index_management/server/routes/api/data_streams/register_get_route.ts b/x-pack/plugins/index_management/server/routes/api/data_streams/register_get_route.ts index 77917db95a1161..9573b9cc6436f2 100644 --- a/x-pack/plugins/index_management/server/routes/api/data_streams/register_get_route.ts +++ b/x-pack/plugins/index_management/server/routes/api/data_streams/register_get_route.ts @@ -91,7 +91,7 @@ const getDataStreamsStats = (client: ElasticsearchClient, name = '*') => { }; const getDataStreamsPrivileges = (client: ElasticsearchClient, names: string[]) => { - return client.security.hasPrivileges({ + return client.security.hasPrivileges({ body: { index: [ { @@ -143,6 +143,7 @@ export function registerGetAllRoute({ dataStreams = enhanceDataStreams({ dataStreams, dataStreamsStats, + // @ts-expect-error PrivilegesFromEs incompatible with ApplicationsPrivileges dataStreamsPrivileges, }); @@ -195,6 +196,7 @@ export function registerGetOneRoute({ const enhancedDataStreams = enhanceDataStreams({ dataStreams, dataStreamsStats, + // @ts-expect-error PrivilegesFromEs incompatible with ApplicationsPrivileges dataStreamsPrivileges, }); const body = deserializeDataStream(enhancedDataStreams[0]); diff --git a/x-pack/plugins/infra/public/utils/logs_overview_fetchers.ts b/x-pack/plugins/infra/public/utils/logs_overview_fetchers.ts index 5369deb1034eef..43f0b12a23f237 100644 --- a/x-pack/plugins/infra/public/utils/logs_overview_fetchers.ts +++ b/x-pack/plugins/infra/public/utils/logs_overview_fetchers.ts @@ -6,7 +6,7 @@ */ import { encode } from 'rison-node'; -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { FetchData, FetchDataParams, LogsFetchDataResponse } from '../../../observability/public'; import { DEFAULT_SOURCE_ID } from '../../common/constants'; import { callFetchLogSourceConfigurationAPI } from '../containers/logs/log_source/api/fetch_log_source_configuration'; @@ -81,7 +81,7 @@ async function fetchLogsOverview( dataPlugin: InfraClientStartDeps['data'] ): Promise { return new Promise((resolve, reject) => { - let esResponse: SearchResponse | undefined; + let esResponse: estypes.SearchResponse | undefined; dataPlugin.search .search({ @@ -99,7 +99,7 @@ async function fetchLogsOverview( (error) => reject(error), () => { if (esResponse?.aggregations) { - resolve(processLogsOverviewAggregations(esResponse!.aggregations)); + resolve(processLogsOverviewAggregations(esResponse!.aggregations as any)); } else { resolve({ stats: {}, series: {} }); } diff --git a/x-pack/plugins/infra/server/lib/adapters/framework/kibana_framework_adapter.ts b/x-pack/plugins/infra/server/lib/adapters/framework/kibana_framework_adapter.ts index 5784afa31ca4f8..9f52192764be58 100644 --- a/x-pack/plugins/infra/server/lib/adapters/framework/kibana_framework_adapter.ts +++ b/x-pack/plugins/infra/server/lib/adapters/framework/kibana_framework_adapter.ts @@ -9,9 +9,9 @@ import { IndicesExistsAlias, IndicesGet, MlGetBuckets, - Msearch, } from '@elastic/elasticsearch/api/requestParams'; import { TransportRequestParams } from '@elastic/elasticsearch/lib/Transport'; +import { estypes } from '@elastic/elasticsearch'; import { InfraRouteConfig, InfraTSVBResponse, @@ -153,7 +153,7 @@ export class KibanaFramework { apiResult = elasticsearch.client.asCurrentUser.msearch({ ...params, ...frozenIndicesParams, - } as Msearch); + } as estypes.MultiSearchRequest); break; case 'fieldCaps': apiResult = elasticsearch.client.asCurrentUser.fieldCaps({ diff --git a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts index 8b7940271d0a05..6893270bbd3430 100644 --- a/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts +++ b/x-pack/plugins/infra/server/lib/alerting/inventory_metric_threshold/evaluate_condition.ts @@ -107,6 +107,7 @@ const getData = async ( const client = async ( options: CallWithRequestParams ): Promise> => + // @ts-expect-error @elastic/elasticsearch SearchResponse.body.timeout is not required (await esClient.search(options as any)).body as InfraDatabaseSearchResponse; const metrics = [ diff --git a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts index b7d3dbb1f7adbd..f6214edc5d0ab9 100644 --- a/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts +++ b/x-pack/plugins/infra/server/lib/alerting/metric_threshold/lib/evaluate_alert.ts @@ -127,6 +127,7 @@ const getMetric: ( (response) => response.aggregations?.groupings?.after_key ); const compositeBuckets = (await getAllCompositeData( + // @ts-expect-error @elastic/elasticsearch SearchResponse.body.timeout is not required (body) => esClient.search({ body, index }), searchBody, bucketSelector, @@ -147,7 +148,12 @@ const getMetric: ( index, }); - return { [UNGROUPED_FACTORY_KEY]: getValuesFromAggregations(result.aggregations, aggType) }; + return { + [UNGROUPED_FACTORY_KEY]: getValuesFromAggregations( + (result.aggregations! as unknown) as Aggregation, + aggType + ), + }; } catch (e) { if (timeframe) { // This code should only ever be reached when previewing the alert, not executing it diff --git a/x-pack/plugins/infra/server/services/log_entries/log_entries_search_strategy.ts b/x-pack/plugins/infra/server/services/log_entries/log_entries_search_strategy.ts index 161685aac29ad4..32bb0596ab5617 100644 --- a/x-pack/plugins/infra/server/services/log_entries/log_entries_search_strategy.ts +++ b/x-pack/plugins/infra/server/services/log_entries/log_entries_search_strategy.ts @@ -98,6 +98,7 @@ export const logEntriesSearchStrategyProvider = ({ map( ([{ configuration }, messageFormattingRules]): IEsSearchRequest => { return { + // @ts-expect-error @elastic/elasticsearch declares indices_boost as Record params: createGetLogEntriesQuery( configuration.logAlias, params.startTimestamp, diff --git a/x-pack/plugins/infra/server/services/log_entries/log_entry_search_strategy.ts b/x-pack/plugins/infra/server/services/log_entries/log_entry_search_strategy.ts index 85eacba823b2bd..b6073f1bbe4c97 100644 --- a/x-pack/plugins/infra/server/services/log_entries/log_entry_search_strategy.ts +++ b/x-pack/plugins/infra/server/services/log_entries/log_entry_search_strategy.ts @@ -65,6 +65,7 @@ export const logEntrySearchStrategyProvider = ({ sourceConfiguration$.pipe( map( ({ configuration }): IEsSearchRequest => ({ + // @ts-expect-error @elastic/elasticsearch declares indices_boost as Record params: createGetLogEntryQuery( configuration.logAlias, params.logEntryId, diff --git a/x-pack/plugins/infra/server/services/log_entries/queries/log_entries.ts b/x-pack/plugins/infra/server/services/log_entries/queries/log_entries.ts index aa640f106d1ee9..6ae7232d77a177 100644 --- a/x-pack/plugins/infra/server/services/log_entries/queries/log_entries.ts +++ b/x-pack/plugins/infra/server/services/log_entries/queries/log_entries.ts @@ -5,7 +5,7 @@ * 2.0. */ -import type { RequestParams } from '@elastic/elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import * as rt from 'io-ts'; import { LogEntryAfterCursor, @@ -31,7 +31,7 @@ export const createGetLogEntriesQuery = ( fields: string[], query?: JsonObject, highlightTerm?: string -): RequestParams.AsyncSearchSubmit> => { +): estypes.AsyncSearchSubmitRequest => { const sortDirection = getSortDirection(cursor); const highlightQuery = createHighlightQuery(highlightTerm, fields); @@ -51,6 +51,7 @@ export const createGetLogEntriesQuery = ( ], }, }, + // @ts-expect-error @elastic/elasticsearch doesn't declare body.fields on AsyncSearchSubmitRequest fields, _source: false, ...createSortClause(sortDirection, timestampField, tiebreakerField), diff --git a/x-pack/plugins/infra/server/services/log_entries/queries/log_entry.ts b/x-pack/plugins/infra/server/services/log_entries/queries/log_entry.ts index 51714be775e977..85af8b92fe080c 100644 --- a/x-pack/plugins/infra/server/services/log_entries/queries/log_entry.ts +++ b/x-pack/plugins/infra/server/services/log_entries/queries/log_entry.ts @@ -5,7 +5,7 @@ * 2.0. */ -import type { RequestParams } from '@elastic/elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import * as rt from 'io-ts'; import { jsonArrayRT } from '../../../../common/typed_json'; import { @@ -18,7 +18,7 @@ export const createGetLogEntryQuery = ( logEntryId: string, timestampField: string, tiebreakerField: string -): RequestParams.AsyncSearchSubmit> => ({ +): estypes.AsyncSearchSubmitRequest => ({ index: logEntryIndex, terminate_after: 1, track_scores: false, @@ -30,6 +30,7 @@ export const createGetLogEntryQuery = ( values: [logEntryId], }, }, + // @ts-expect-error @elastic/elasticsearch doesn't declare body.fields on AsyncSearchSubmitRequest fields: ['*'], sort: [{ [timestampField]: 'desc' }, { [tiebreakerField]: 'desc' }], _source: false, diff --git a/x-pack/plugins/lens/server/routes/existing_fields.test.ts b/x-pack/plugins/lens/server/routes/existing_fields.test.ts index 3f3e94099f6669..57d8ebf678d614 100644 --- a/x-pack/plugins/lens/server/routes/existing_fields.test.ts +++ b/x-pack/plugins/lens/server/routes/existing_fields.test.ts @@ -22,7 +22,7 @@ describe('existingFields', () => { } function searchResults(fields: Record = {}) { - return { fields }; + return { fields, _index: '_index', _id: '_id' }; } it('should handle root level fields', () => { @@ -77,7 +77,13 @@ describe('existingFields', () => { it('supports meta fields', () => { const result = existingFields( - [{ _mymeta: 'abc', ...searchResults({ bar: ['scriptvalue'] }) }], + [ + { + // @ts-expect-error _mymeta is not defined on estypes.Hit + _mymeta: 'abc', + ...searchResults({ bar: ['scriptvalue'] }), + }, + ], [field({ name: '_mymeta', isMeta: true })] ); diff --git a/x-pack/plugins/lens/server/routes/existing_fields.ts b/x-pack/plugins/lens/server/routes/existing_fields.ts index 8a2db992a839da..2e6d6128352315 100644 --- a/x-pack/plugins/lens/server/routes/existing_fields.ts +++ b/x-pack/plugins/lens/server/routes/existing_fields.ts @@ -6,7 +6,7 @@ */ import Boom from '@hapi/boom'; -import { errors } from '@elastic/elasticsearch'; +import { errors, estypes } from '@elastic/elasticsearch'; import { schema } from '@kbn/config-schema'; import { RequestHandlerContext, ElasticsearchClient } from 'src/core/server'; import { CoreSetup, Logger } from 'src/core/server'; @@ -192,18 +192,19 @@ async function fetchIndexPatternStats({ _source: false, runtime_mappings: runtimeFields.reduce((acc, field) => { if (!field.runtimeField) return acc; + // @ts-expect-error @elastic/elasticsearch StoredScript.language is required acc[field.name] = field.runtimeField; return acc; - }, {} as Record), + }, {} as Record), script_fields: scriptedFields.reduce((acc, field) => { acc[field.name] = { script: { - lang: field.lang, - source: field.script, + lang: field.lang!, + source: field.script!, }, }; return acc; - }, {} as Record), + }, {} as Record), }, }); return result.hits.hits; @@ -212,10 +213,7 @@ async function fetchIndexPatternStats({ /** * Exported only for unit tests. */ -export function existingFields( - docs: Array<{ fields: Record; [key: string]: unknown }>, - fields: Field[] -): string[] { +export function existingFields(docs: estypes.Hit[], fields: Field[]): string[] { const missingFields = new Set(fields); for (const doc of docs) { @@ -224,7 +222,7 @@ export function existingFields( } missingFields.forEach((field) => { - let fieldStore: Record = doc.fields; + let fieldStore = doc.fields!; if (field.isMeta) { fieldStore = doc; } diff --git a/x-pack/plugins/lens/server/routes/field_stats.ts b/x-pack/plugins/lens/server/routes/field_stats.ts index 49ea8c2076f7aa..6cddd2c60f4165 100644 --- a/x-pack/plugins/lens/server/routes/field_stats.ts +++ b/x-pack/plugins/lens/server/routes/field_stats.ts @@ -4,8 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - -import { errors } from '@elastic/elasticsearch'; +import { errors, estypes } from '@elastic/elasticsearch'; import DateMath from '@elastic/datemath'; import { schema } from '@kbn/config-schema'; import { CoreSetup } from 'src/core/server'; @@ -79,13 +78,14 @@ export async function initFieldsRoute(setup: CoreSetup) { }, }; - const search = async (aggs: unknown) => { + const search = async (aggs: Record) => { const { body: result } = await requestClient.search({ index: indexPattern.title, track_total_hits: true, body: { query, aggs, + // @ts-expect-error @elastic/elasticsearch StoredScript.language is required runtime_mappings: field.runtimeField ? { [fieldName]: field.runtimeField } : {}, }, size: 0, @@ -135,7 +135,7 @@ export async function initFieldsRoute(setup: CoreSetup) { } export async function getNumberHistogram( - aggSearchWithBody: (body: unknown) => Promise, + aggSearchWithBody: (aggs: Record) => Promise, field: IFieldType, useTopHits = true ): Promise { @@ -179,7 +179,10 @@ export async function getNumberHistogram( const terms = 'top_values' in minMaxResult.aggregations!.sample ? minMaxResult.aggregations!.sample.top_values - : { buckets: [] }; + : { + buckets: [] as Array<{ doc_count: number; key: string | number }>, + }; + const topValuesBuckets = { buckets: terms.buckets.map((bucket) => ({ count: bucket.doc_count, @@ -241,7 +244,7 @@ export async function getNumberHistogram( } export async function getStringSamples( - aggSearchWithBody: (body: unknown) => unknown, + aggSearchWithBody: (aggs: Record) => unknown, field: IFieldType ): Promise { const fieldRef = getFieldRef(field); @@ -280,7 +283,7 @@ export async function getStringSamples( // This one is not sampled so that it returns the full date range export async function getDateHistogram( - aggSearchWithBody: (body: unknown) => unknown, + aggSearchWithBody: (aggs: Record) => unknown, field: IFieldType, range: { fromDate: string; toDate: string } ): Promise { diff --git a/x-pack/plugins/lens/server/usage/task.ts b/x-pack/plugins/lens/server/usage/task.ts index d583e1628cbe8e..9c9ab7fd0b350f 100644 --- a/x-pack/plugins/lens/server/usage/task.ts +++ b/x-pack/plugins/lens/server/usage/task.ts @@ -137,14 +137,17 @@ export async function getDailyEvents( const byDateByType: Record> = {}; const suggestionsByDate: Record> = {}; + // @ts-expect-error no way to declare aggregations for search response metrics.aggregations!.daily.buckets.forEach((daily) => { const byType: Record = byDateByType[daily.key] || {}; + // @ts-expect-error no way to declare aggregations for search response daily.groups.buckets.regularEvents.names.buckets.forEach((bucket) => { byType[bucket.key] = (bucket.sums.value || 0) + (byType[daily.key] || 0); }); byDateByType[daily.key] = byType; const suggestionsByType: Record = suggestionsByDate[daily.key] || {}; + // @ts-expect-error no way to declare aggregations for search response daily.groups.buckets.suggestionEvents.names.buckets.forEach((bucket) => { suggestionsByType[bucket.key] = (bucket.sums.value || 0) + (suggestionsByType[daily.key] || 0); diff --git a/x-pack/plugins/lens/server/usage/visualization_counts.ts b/x-pack/plugins/lens/server/usage/visualization_counts.ts index 5b084ecfef5e41..3b9bb99caf5b81 100644 --- a/x-pack/plugins/lens/server/usage/visualization_counts.ts +++ b/x-pack/plugins/lens/server/usage/visualization_counts.ts @@ -50,6 +50,7 @@ export async function getVisualizationCounts( }, }); + // @ts-expect-error @elastic/elasticsearch no way to declare aggregations for search response const buckets = results.aggregations.groups.buckets; // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/x-pack/plugins/lists/server/services/items/create_list_item.test.ts b/x-pack/plugins/lists/server/services/items/create_list_item.test.ts index a13163d8f774a5..acfed44c5259e2 100644 --- a/x-pack/plugins/lists/server/services/items/create_list_item.test.ts +++ b/x-pack/plugins/lists/server/services/items/create_list_item.test.ts @@ -28,6 +28,7 @@ describe('crete_list_item', () => { const options = getCreateListItemOptionsMock(); const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser; esClient.index.mockReturnValue( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ _id: 'elastic-id-123' }) ); const listItem = await createListItem({ ...options, esClient }); @@ -54,6 +55,7 @@ describe('crete_list_item', () => { options.id = undefined; const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser; esClient.index.mockReturnValue( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ _id: 'elastic-id-123' }) ); const list = await createListItem({ ...options, esClient }); diff --git a/x-pack/plugins/lists/server/services/items/create_list_item.ts b/x-pack/plugins/lists/server/services/items/create_list_item.ts index a5369bbfe7ca41..cf8a43be796dff 100644 --- a/x-pack/plugins/lists/server/services/items/create_list_item.ts +++ b/x-pack/plugins/lists/server/services/items/create_list_item.ts @@ -6,7 +6,6 @@ */ import uuid from 'uuid'; -import { CreateDocumentResponse } from 'elasticsearch'; import { ElasticsearchClient } from 'kibana/server'; import { @@ -69,7 +68,7 @@ export const createListItem = async ({ ...baseBody, ...elasticQuery, }; - const { body: response } = await esClient.index({ + const { body: response } = await esClient.index({ body, id, index: listItemIndex, diff --git a/x-pack/plugins/lists/server/services/items/find_list_item.test.ts b/x-pack/plugins/lists/server/services/items/find_list_item.test.ts index 29e6f2f8450023..c76d1c505df0c4 100644 --- a/x-pack/plugins/lists/server/services/items/find_list_item.test.ts +++ b/x-pack/plugins/lists/server/services/items/find_list_item.test.ts @@ -20,9 +20,11 @@ describe('find_list_item', () => { const options = getFindListItemOptionsMock(); const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser; esClient.count.mockReturnValue( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ count: 1 }) ); esClient.search.mockReturnValue( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ _scroll_id: '123', _shards: getShardMock(), diff --git a/x-pack/plugins/lists/server/services/items/find_list_item.ts b/x-pack/plugins/lists/server/services/items/find_list_item.ts index 727c55d53e459d..3e37ccb0cfb1f3 100644 --- a/x-pack/plugins/lists/server/services/items/find_list_item.ts +++ b/x-pack/plugins/lists/server/services/items/find_list_item.ts @@ -6,7 +6,6 @@ */ import { ElasticsearchClient } from 'kibana/server'; -import { SearchResponse } from 'elasticsearch'; import { Filter, @@ -75,8 +74,9 @@ export const findListItem = async ({ sortOrder, }); - const { body: respose } = await esClient.count<{ count: number }>({ + const { body: respose } = await esClient.count({ body: { + // @ts-expect-error GetQueryFilterReturn is not assignable to QueryContainer query, }, ignore_unavailable: true, @@ -87,8 +87,9 @@ export const findListItem = async ({ // Note: This typing of response = await esClient> // is because when you pass in seq_no_primary_term: true it does a "fall through" type and you have // to explicitly define the type . - const { body: response } = await esClient.search>({ + const { body: response } = await esClient.search({ body: { + // @ts-expect-error GetQueryFilterReturn is not assignable to QueryContainer query, search_after: scroll.searchAfter, sort: getSortWithTieBreaker({ sortField, sortOrder }), diff --git a/x-pack/plugins/lists/server/services/items/get_list_item.ts b/x-pack/plugins/lists/server/services/items/get_list_item.ts index eb05a899478a53..519ebaedfddbc7 100644 --- a/x-pack/plugins/lists/server/services/items/get_list_item.ts +++ b/x-pack/plugins/lists/server/services/items/get_list_item.ts @@ -6,7 +6,6 @@ */ import { ElasticsearchClient } from 'kibana/server'; -import { SearchResponse } from 'elasticsearch'; import { Id, ListItemSchema, SearchEsListItemSchema } from '../../../common/schemas'; import { transformElasticToListItem } from '../utils'; @@ -26,7 +25,7 @@ export const getListItem = async ({ // Note: This typing of response = await esClient> // is because when you pass in seq_no_primary_term: true it does a "fall through" type and you have // to explicitly define the type . - const { body: listItemES } = await esClient.search>({ + const { body: listItemES } = await esClient.search({ body: { query: { term: { @@ -40,6 +39,7 @@ export const getListItem = async ({ }); if (listItemES.hits.hits.length) { + // @ts-expect-error @elastic/elasticsearch _source is optional const type = findSourceType(listItemES.hits.hits[0]._source); if (type != null) { const listItems = transformElasticToListItem({ response: listItemES, type }); diff --git a/x-pack/plugins/lists/server/services/items/update_list_item.test.ts b/x-pack/plugins/lists/server/services/items/update_list_item.test.ts index ae6b6ad3faecf9..195bce879f34d6 100644 --- a/x-pack/plugins/lists/server/services/items/update_list_item.test.ts +++ b/x-pack/plugins/lists/server/services/items/update_list_item.test.ts @@ -34,6 +34,7 @@ describe('update_list_item', () => { const options = getUpdateListItemOptionsMock(); const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser; esClient.update.mockReturnValue( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ _id: 'elastic-id-123' }) ); const updatedList = await updateListItem({ ...options, esClient }); diff --git a/x-pack/plugins/lists/server/services/items/update_list_item.ts b/x-pack/plugins/lists/server/services/items/update_list_item.ts index 645508691acc81..89c7e77707d8fd 100644 --- a/x-pack/plugins/lists/server/services/items/update_list_item.ts +++ b/x-pack/plugins/lists/server/services/items/update_list_item.ts @@ -5,7 +5,6 @@ * 2.0. */ -import { CreateDocumentResponse } from 'elasticsearch'; import { ElasticsearchClient } from 'kibana/server'; import { @@ -62,7 +61,7 @@ export const updateListItem = async ({ ...elasticQuery, }; - const { body: response } = await esClient.update({ + const { body: response } = await esClient.update({ ...decodeVersion(_version), body: { doc, diff --git a/x-pack/plugins/lists/server/services/items/write_list_items_to_stream.test.ts b/x-pack/plugins/lists/server/services/items/write_list_items_to_stream.test.ts index b096adb2d1a132..ee4f3af9cdd5c0 100644 --- a/x-pack/plugins/lists/server/services/items/write_list_items_to_stream.test.ts +++ b/x-pack/plugins/lists/server/services/items/write_list_items_to_stream.test.ts @@ -305,7 +305,9 @@ describe('write_list_items_to_stream', () => { test('it will throw an exception with a status code if the hit_source is not a data type we expect', () => { const options = getWriteResponseHitsToStreamOptionsMock(); + // @ts-expect-error _source is optional options.response.hits.hits[0]._source.ip = undefined; + // @ts-expect-error _source is optional options.response.hits.hits[0]._source.keyword = undefined; const expected = `Encountered an error where hit._source was an unexpected type: ${JSON.stringify( options.response.hits.hits[0]._source diff --git a/x-pack/plugins/lists/server/services/items/write_list_items_to_stream.ts b/x-pack/plugins/lists/server/services/items/write_list_items_to_stream.ts index 9bdcb58835ab0b..3679680ad79bd1 100644 --- a/x-pack/plugins/lists/server/services/items/write_list_items_to_stream.ts +++ b/x-pack/plugins/lists/server/services/items/write_list_items_to_stream.ts @@ -7,7 +7,7 @@ import { PassThrough } from 'stream'; -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { ElasticsearchClient } from 'kibana/server'; import { SearchEsListItemSchema } from '../../../common/schemas'; @@ -95,8 +95,9 @@ export const writeNextResponse = async ({ export const getSearchAfterFromResponse = ({ response, }: { - response: SearchResponse; + response: estypes.SearchResponse; }): string[] | undefined => + // @ts-expect-error @elastic/elasticsearch SortResults contains null response.hits.hits.length > 0 ? response.hits.hits[response.hits.hits.length - 1].sort : undefined; @@ -115,7 +116,7 @@ export const getResponse = async ({ listId, listItemIndex, size = SIZE, -}: GetResponseOptions): Promise> => { +}: GetResponseOptions): Promise> => { return (( await esClient.search({ body: { @@ -131,11 +132,11 @@ export const getResponse = async ({ index: listItemIndex, size, }) - ).body as unknown) as SearchResponse; + ).body as unknown) as estypes.SearchResponse; }; export interface WriteResponseHitsToStreamOptions { - response: SearchResponse; + response: estypes.SearchResponse; stream: PassThrough; stringToAppend: string | null | undefined; } @@ -148,6 +149,7 @@ export const writeResponseHitsToStream = ({ const stringToAppendOrEmpty = stringToAppend ?? ''; response.hits.hits.forEach((hit) => { + // @ts-expect-error @elastic/elasticsearch _source is optional const value = findSourceValue(hit._source); if (value != null) { stream.push(`${value}${stringToAppendOrEmpty}`); diff --git a/x-pack/plugins/lists/server/services/lists/create_list.test.ts b/x-pack/plugins/lists/server/services/lists/create_list.test.ts index 6fc556955fae3b..e6213a1c6eabe9 100644 --- a/x-pack/plugins/lists/server/services/lists/create_list.test.ts +++ b/x-pack/plugins/lists/server/services/lists/create_list.test.ts @@ -29,6 +29,7 @@ describe('crete_list', () => { const options = getCreateListOptionsMock(); const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser; esClient.index.mockReturnValue( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ _id: 'elastic-id-123' }) ); const list = await createList({ ...options, esClient }); @@ -44,6 +45,7 @@ describe('crete_list', () => { }; const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser; esClient.index.mockReturnValue( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ _id: 'elastic-id-123' }) ); const list = await createList({ ...options, esClient }); @@ -74,6 +76,7 @@ describe('crete_list', () => { options.id = undefined; const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser; esClient.index.mockReturnValue( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ _id: 'elastic-id-123' }) ); const list = await createList({ ...options, esClient }); diff --git a/x-pack/plugins/lists/server/services/lists/create_list.ts b/x-pack/plugins/lists/server/services/lists/create_list.ts index 2671a23266ec96..baed699dc992fa 100644 --- a/x-pack/plugins/lists/server/services/lists/create_list.ts +++ b/x-pack/plugins/lists/server/services/lists/create_list.ts @@ -6,7 +6,6 @@ */ import uuid from 'uuid'; -import { CreateDocumentResponse } from 'elasticsearch'; import { ElasticsearchClient } from 'kibana/server'; import { encodeHitVersion } from '../utils/encode_hit_version'; @@ -73,7 +72,7 @@ export const createList = async ({ updated_by: user, version, }; - const { body: response } = await esClient.index({ + const { body: response } = await esClient.index({ body, id, index: listIndex, diff --git a/x-pack/plugins/lists/server/services/lists/find_list.ts b/x-pack/plugins/lists/server/services/lists/find_list.ts index c5a398b0a1ad04..9c61d36dc0cd35 100644 --- a/x-pack/plugins/lists/server/services/lists/find_list.ts +++ b/x-pack/plugins/lists/server/services/lists/find_list.ts @@ -6,7 +6,6 @@ */ import { ElasticsearchClient } from 'kibana/server'; -import { SearchResponse } from 'elasticsearch'; import { Filter, @@ -66,6 +65,7 @@ export const findList = async ({ const { body: totalCount } = await esClient.count({ body: { + // @ts-expect-error GetQueryFilterReturn is not compatible with QueryContainer query, }, ignore_unavailable: true, @@ -76,8 +76,9 @@ export const findList = async ({ // Note: This typing of response = await esClient> // is because when you pass in seq_no_primary_term: true it does a "fall through" type and you have // to explicitly define the type . - const { body: response } = await esClient.search>({ + const { body: response } = await esClient.search({ body: { + // @ts-expect-error GetQueryFilterReturn is not compatible with QueryContainer query, search_after: scroll.searchAfter, sort: getSortWithTieBreaker({ sortField, sortOrder }), diff --git a/x-pack/plugins/lists/server/services/lists/get_list.ts b/x-pack/plugins/lists/server/services/lists/get_list.ts index 50e6d08dd80ffd..6f18d143df00bc 100644 --- a/x-pack/plugins/lists/server/services/lists/get_list.ts +++ b/x-pack/plugins/lists/server/services/lists/get_list.ts @@ -6,7 +6,6 @@ */ import { ElasticsearchClient } from 'kibana/server'; -import { SearchResponse } from 'elasticsearch'; import { Id, ListSchema, SearchEsListSchema } from '../../../common/schemas'; import { transformElasticToList } from '../utils/transform_elastic_to_list'; @@ -25,7 +24,7 @@ export const getList = async ({ // Note: This typing of response = await esClient> // is because when you pass in seq_no_primary_term: true it does a "fall through" type and you have // to explicitly define the type . - const { body: response } = await esClient.search>({ + const { body: response } = await esClient.search({ body: { query: { term: { diff --git a/x-pack/plugins/lists/server/services/lists/update_list.test.ts b/x-pack/plugins/lists/server/services/lists/update_list.test.ts index e2d3b09fe518ac..8cc1c60ecc23dd 100644 --- a/x-pack/plugins/lists/server/services/lists/update_list.test.ts +++ b/x-pack/plugins/lists/server/services/lists/update_list.test.ts @@ -34,6 +34,7 @@ describe('update_list', () => { const options = getUpdateListOptionsMock(); const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser; esClient.update.mockReturnValue( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ _id: 'elastic-id-123' }) ); const updatedList = await updateList({ ...options, esClient }); @@ -51,6 +52,7 @@ describe('update_list', () => { const options = getUpdateListOptionsMock(); const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser; esClient.update.mockReturnValue( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ _id: 'elastic-id-123' }) ); const updatedList = await updateList({ ...options, esClient }); diff --git a/x-pack/plugins/lists/server/services/lists/update_list.ts b/x-pack/plugins/lists/server/services/lists/update_list.ts index aa4eb9a8d834fd..f98e40b04b6d73 100644 --- a/x-pack/plugins/lists/server/services/lists/update_list.ts +++ b/x-pack/plugins/lists/server/services/lists/update_list.ts @@ -5,7 +5,6 @@ * 2.0. */ -import { CreateDocumentResponse } from 'elasticsearch'; import { ElasticsearchClient } from 'kibana/server'; import { decodeVersion } from '../utils/decode_version'; @@ -61,7 +60,7 @@ export const updateList = async ({ updated_at: updatedAt, updated_by: user, }; - const { body: response } = await esClient.update({ + const { body: response } = await esClient.update({ ...decodeVersion(_version), body: { doc }, id, diff --git a/x-pack/plugins/lists/server/services/utils/get_search_after_scroll.ts b/x-pack/plugins/lists/server/services/utils/get_search_after_scroll.ts index 34359a7a9c697e..ae37e47861845a 100644 --- a/x-pack/plugins/lists/server/services/utils/get_search_after_scroll.ts +++ b/x-pack/plugins/lists/server/services/utils/get_search_after_scroll.ts @@ -6,7 +6,6 @@ */ import { ElasticsearchClient } from 'kibana/server'; -import { SearchResponse } from 'elasticsearch'; import { Filter, SortFieldOrUndefined, SortOrderOrUndefined } from '../../../common/schemas'; import { Scroll } from '../lists/types'; @@ -40,9 +39,10 @@ export const getSearchAfterScroll = async ({ const query = getQueryFilter({ filter }); let newSearchAfter = searchAfter; for (let i = 0; i < hops; ++i) { - const { body: response } = await esClient.search>>({ + const { body: response } = await esClient.search>({ body: { _source: getSourceWithTieBreaker({ sortField }), + // @ts-expect-error Filter is not assignale to QueryContainer query, search_after: newSearchAfter, sort: getSortWithTieBreaker({ sortField, sortOrder }), diff --git a/x-pack/plugins/lists/server/services/utils/get_search_after_with_tie_breaker.ts b/x-pack/plugins/lists/server/services/utils/get_search_after_with_tie_breaker.ts index 87749ed6fdb3b2..3cd902aeeb36e5 100644 --- a/x-pack/plugins/lists/server/services/utils/get_search_after_with_tie_breaker.ts +++ b/x-pack/plugins/lists/server/services/utils/get_search_after_with_tie_breaker.ts @@ -4,8 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { SortFieldOrUndefined } from '../../../common/schemas'; @@ -14,7 +13,7 @@ export type TieBreaker = T & { }; interface GetSearchAfterWithTieBreakerOptions { - response: SearchResponse>; + response: estypes.SearchResponse>; sortField: SortFieldOrUndefined; } @@ -27,14 +26,18 @@ export const getSearchAfterWithTieBreaker = ({ } else { const lastEsElement = response.hits.hits[response.hits.hits.length - 1]; if (sortField == null) { + // @ts-expect-error @elastic/elasticsearch _source is optional return [lastEsElement._source.tie_breaker_id]; } else { - const [[, sortValue]] = Object.entries(lastEsElement._source).filter( - ([key]) => key === sortField - ); + const [[, sortValue]] = Object.entries( + // @ts-expect-error @elastic/elasticsearch _source is optional + lastEsElement._source + ).filter(([key]) => key === sortField); if (typeof sortValue === 'string') { + // @ts-expect-error @elastic/elasticsearch _source is optional return [sortValue, lastEsElement._source.tie_breaker_id]; } else { + // @ts-expect-error @elastic/elasticsearch _source is optional return [lastEsElement._source.tie_breaker_id]; } } diff --git a/x-pack/plugins/lists/server/services/utils/get_sort_with_tie_breaker.ts b/x-pack/plugins/lists/server/services/utils/get_sort_with_tie_breaker.ts index 3fd886f8f6919e..97cfe3dd8e6347 100644 --- a/x-pack/plugins/lists/server/services/utils/get_sort_with_tie_breaker.ts +++ b/x-pack/plugins/lists/server/services/utils/get_sort_with_tie_breaker.ts @@ -4,25 +4,21 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ +import type { estypes } from '@elastic/elasticsearch'; import { SortFieldOrUndefined, SortOrderOrUndefined } from '../../../common/schemas'; -export interface SortWithTieBreakerReturn { - tie_breaker_id: 'asc'; - [key: string]: string; -} - export const getSortWithTieBreaker = ({ sortField, sortOrder, }: { sortField: SortFieldOrUndefined; sortOrder: SortOrderOrUndefined; -}): SortWithTieBreakerReturn[] | undefined => { - const ascOrDesc = sortOrder ?? 'asc'; +}): estypes.SortCombinations[] => { + const ascOrDesc = sortOrder ?? ('asc' as const); if (sortField != null) { - return [{ [sortField]: ascOrDesc, tie_breaker_id: 'asc' }]; + return [{ [sortField]: ascOrDesc, tie_breaker_id: 'asc' as const }]; } else { - return [{ tie_breaker_id: 'asc' }]; + return [{ tie_breaker_id: 'asc' as const }]; } }; diff --git a/x-pack/plugins/lists/server/services/utils/transform_elastic_named_search_to_list_item.ts b/x-pack/plugins/lists/server/services/utils/transform_elastic_named_search_to_list_item.ts index 3dd0f083797f10..4f0f8fe49a9d0c 100644 --- a/x-pack/plugins/lists/server/services/utils/transform_elastic_named_search_to_list_item.ts +++ b/x-pack/plugins/lists/server/services/utils/transform_elastic_named_search_to_list_item.ts @@ -5,14 +5,14 @@ * 2.0. */ -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { SearchEsListItemSchema, SearchListItemArraySchema, Type } from '../../../common/schemas'; import { transformElasticHitsToListItem } from './transform_elastic_to_list_item'; export interface TransformElasticMSearchToListItemOptions { - response: SearchResponse; + response: estypes.SearchResponse; type: Type; value: unknown[]; } diff --git a/x-pack/plugins/lists/server/services/utils/transform_elastic_to_list.ts b/x-pack/plugins/lists/server/services/utils/transform_elastic_to_list.ts index fa77336fb7724e..4ed08f70219aff 100644 --- a/x-pack/plugins/lists/server/services/utils/transform_elastic_to_list.ts +++ b/x-pack/plugins/lists/server/services/utils/transform_elastic_to_list.ts @@ -5,19 +5,20 @@ * 2.0. */ -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { ListArraySchema, SearchEsListSchema } from '../../../common/schemas'; import { encodeHitVersion } from './encode_hit_version'; export interface TransformElasticToListOptions { - response: SearchResponse; + response: estypes.SearchResponse; } export const transformElasticToList = ({ response, }: TransformElasticToListOptions): ListArraySchema => { + // @ts-expect-error created_at is incompatible return response.hits.hits.map((hit) => { return { _version: encodeHitVersion(hit), diff --git a/x-pack/plugins/lists/server/services/utils/transform_elastic_to_list_item.ts b/x-pack/plugins/lists/server/services/utils/transform_elastic_to_list_item.ts index 8c1949ed90cdad..436987e71dd221 100644 --- a/x-pack/plugins/lists/server/services/utils/transform_elastic_to_list_item.ts +++ b/x-pack/plugins/lists/server/services/utils/transform_elastic_to_list_item.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { ListItemArraySchema, SearchEsListItemSchema, Type } from '../../../common/schemas'; import { ErrorWithStatusCode } from '../../error_with_status_code'; @@ -14,12 +14,12 @@ import { encodeHitVersion } from './encode_hit_version'; import { findSourceValue } from './find_source_value'; export interface TransformElasticToListItemOptions { - response: SearchResponse; + response: estypes.SearchResponse; type: Type; } export interface TransformElasticHitToListItemOptions { - hits: SearchResponse['hits']['hits']; + hits: Array>; type: Type; } @@ -35,22 +35,21 @@ export const transformElasticHitsToListItem = ({ type, }: TransformElasticHitToListItemOptions): ListItemArraySchema => { return hits.map((hit) => { + const { _id, _source } = hit; const { - _id, - _source: { - /* eslint-disable @typescript-eslint/naming-convention */ - created_at, - deserializer, - serializer, - updated_at, - updated_by, - created_by, - list_id, - tie_breaker_id, - meta, - /* eslint-enable @typescript-eslint/naming-convention */ - }, - } = hit; + /* eslint-disable @typescript-eslint/naming-convention */ + created_at, + deserializer, + serializer, + updated_at, + updated_by, + created_by, + list_id, + tie_breaker_id, + meta, + /* eslint-enable @typescript-eslint/naming-convention */ + } = _source!; // eslint-disable-line @typescript-eslint/no-non-null-assertion + // @ts-expect-error _source is optional const value = findSourceValue(hit._source); if (value == null) { throw new ErrorWithStatusCode(`Was expected ${type} to not be null/undefined`, 400); diff --git a/x-pack/plugins/maps/public/classes/sources/es_source/es_source.ts b/x-pack/plugins/maps/public/classes/sources/es_source/es_source.ts index 0936cdc50b4c03..c55a564951c4ec 100644 --- a/x-pack/plugins/maps/public/classes/sources/es_source/es_source.ts +++ b/x-pack/plugins/maps/public/classes/sources/es_source/es_source.ts @@ -285,11 +285,29 @@ export class AbstractESSource extends AbstractVectorSource implements IESSource const abortController = new AbortController(); registerCancelCallback(() => abortController.abort()); const esResp = await searchSource.fetch({ abortSignal: abortController.signal }); - if (!esResp.aggregations.fitToBounds.bounds) { + + if (!esResp.aggregations) { + return null; + } + + const fitToBounds = esResp.aggregations.fitToBounds as { + bounds?: { + top_left: { + lat: number; + lon: number; + }; + bottom_right: { + lat: number; + lon: number; + }; + }; + }; + + if (!fitToBounds.bounds) { // aggregations.fitToBounds is empty object when there are no matching documents return null; } - esBounds = esResp.aggregations.fitToBounds.bounds; + esBounds = fitToBounds.bounds; } catch (error) { if (error.name === 'AbortError') { throw new DataRequestAbortError(); diff --git a/x-pack/plugins/maps/server/mvt/get_tile.ts b/x-pack/plugins/maps/server/mvt/get_tile.ts index 3274261cdba56a..d6ebf2fb216b2a 100644 --- a/x-pack/plugins/maps/server/mvt/get_tile.ts +++ b/x-pack/plugins/maps/server/mvt/get_tile.ts @@ -179,6 +179,7 @@ export async function getTile({ [KBN_TOO_MANY_FEATURES_PROPERTY]: true, }, geometry: esBboxToGeoJsonPolygon( + // @ts-expect-error @elastic/elasticsearch no way to declare aggregations for search response bboxResponse.rawResponse.aggregations.data_bounds.bounds, tileToESBbox(x, y, z) ), @@ -199,6 +200,7 @@ export async function getTile({ // Todo: pass in epochMillies-fields const featureCollection = hitsToGeoJson( + // @ts-expect-error hitsToGeoJson should be refactored to accept estypes.Hit documentsResponse.rawResponse.hits.hits, (hit: Record) => { return flattenHit(geometryFieldName, hit); diff --git a/x-pack/plugins/ml/common/types/anomaly_detection_jobs/datafeed.ts b/x-pack/plugins/ml/common/types/anomaly_detection_jobs/datafeed.ts index ed9c9e75897497..77d453b68edc53 100644 --- a/x-pack/plugins/ml/common/types/anomaly_detection_jobs/datafeed.ts +++ b/x-pack/plugins/ml/common/types/anomaly_detection_jobs/datafeed.ts @@ -5,33 +5,41 @@ * 2.0. */ -import { IndexPatternTitle } from '../kibana'; -import { RuntimeMappings } from '../fields'; -import { JobId } from './job'; +import { estypes } from '@elastic/elasticsearch'; +// import { IndexPatternTitle } from '../kibana'; +// import { RuntimeMappings } from '../fields'; +// import { JobId } from './job'; export type DatafeedId = string; -export interface Datafeed { - datafeed_id: DatafeedId; - aggregations?: Aggregation; - aggs?: Aggregation; - chunking_config?: ChunkingConfig; - frequency?: string; - indices: IndexPatternTitle[]; - indexes?: IndexPatternTitle[]; // The datafeed can contain indexes and indices - job_id: JobId; - query: object; - query_delay?: string; - script_fields?: Record; - runtime_mappings?: RuntimeMappings; - scroll_size?: number; - delayed_data_check_config?: object; - indices_options?: IndicesOptions; -} +export type Datafeed = estypes.Datafeed; +// export interface Datafeed extends estypes.DatafeedConfig { +// runtime_mappings?: RuntimeMappings; +// aggs?: Aggregation; +// } +// export interface Datafeed { +// datafeed_id: DatafeedId; +// aggregations?: Aggregation; +// aggs?: Aggregation; +// chunking_config?: ChunkingConfig; +// frequency?: string; +// indices: IndexPatternTitle[]; +// indexes?: IndexPatternTitle[]; // The datafeed can contain indexes and indices +// job_id: JobId; +// query: object; +// query_delay?: string; +// script_fields?: Record; +// runtime_mappings?: RuntimeMappings; +// scroll_size?: number; +// delayed_data_check_config?: object; +// indices_options?: IndicesOptions; +// } -export interface ChunkingConfig { - mode: 'auto' | 'manual' | 'off'; - time_span?: string; -} +export type ChunkingConfig = estypes.ChunkingConfig; + +// export interface ChunkingConfig { +// mode: 'auto' | 'manual' | 'off'; +// time_span?: string; +// } export type Aggregation = Record< string, @@ -45,9 +53,10 @@ export type Aggregation = Record< } >; -export interface IndicesOptions { - expand_wildcards?: 'all' | 'open' | 'closed' | 'hidden' | 'none'; - ignore_unavailable?: boolean; - allow_no_indices?: boolean; - ignore_throttled?: boolean; -} +export type IndicesOptions = estypes.IndicesOptions; +// export interface IndicesOptions { +// expand_wildcards?: 'all' | 'open' | 'closed' | 'hidden' | 'none'; +// ignore_unavailable?: boolean; +// allow_no_indices?: boolean; +// ignore_throttled?: boolean; +// } diff --git a/x-pack/plugins/ml/common/types/anomaly_detection_jobs/job.ts b/x-pack/plugins/ml/common/types/anomaly_detection_jobs/job.ts index a4b0a5c5c60685..5e1d5e009a7642 100644 --- a/x-pack/plugins/ml/common/types/anomaly_detection_jobs/job.ts +++ b/x-pack/plugins/ml/common/types/anomaly_detection_jobs/job.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; import { UrlConfig } from '../custom_urls'; import { CREATED_BY_LABEL } from '../../constants/new_job'; @@ -19,79 +20,87 @@ export interface CustomSettings { }; } -export interface Job { - job_id: JobId; - analysis_config: AnalysisConfig; - analysis_limits?: AnalysisLimits; - background_persist_interval?: string; - custom_settings?: CustomSettings; - data_description: DataDescription; - description: string; - groups: string[]; - model_plot_config?: ModelPlotConfig; - model_snapshot_retention_days?: number; - daily_model_snapshot_retention_after_days?: number; - renormalization_window_days?: number; - results_index_name?: string; - results_retention_days?: number; +export type Job = estypes.Job; +// export interface Job { +// job_id: JobId; +// analysis_config: AnalysisConfig; +// analysis_limits?: AnalysisLimits; +// background_persist_interval?: string; +// custom_settings?: CustomSettings; +// data_description: DataDescription; +// description: string; +// groups: string[]; +// model_plot_config?: ModelPlotConfig; +// model_snapshot_retention_days?: number; +// daily_model_snapshot_retention_after_days?: number; +// renormalization_window_days?: number; +// results_index_name?: string; +// results_retention_days?: number; - // optional properties added when the job has been created - create_time?: number; - finished_time?: number; - job_type?: 'anomaly_detector'; - job_version?: string; - model_snapshot_id?: string; - deleting?: boolean; -} +// // optional properties added when the job has been created +// create_time?: number; +// finished_time?: number; +// job_type?: 'anomaly_detector'; +// job_version?: string; +// model_snapshot_id?: string; +// deleting?: boolean; +// } -export interface AnalysisConfig { - bucket_span: BucketSpan; - categorization_field_name?: string; - categorization_filters?: string[]; - categorization_analyzer?: object | string; - detectors: Detector[]; - influencers: string[]; - latency?: number; - multivariate_by_fields?: boolean; - summary_count_field_name?: string; - per_partition_categorization?: PerPartitionCategorization; -} +export type AnalysisConfig = estypes.AnalysisConfig; +// export interface AnalysisConfig { +// bucket_span: BucketSpan; +// categorization_field_name?: string; +// categorization_filters?: string[]; +// categorization_analyzer?: object | string; +// detectors: Detector[]; +// influencers: string[]; +// latency?: number; +// multivariate_by_fields?: boolean; +// summary_count_field_name?: string; +// per_partition_categorization?: PerPartitionCategorization; +// } -export interface Detector { - by_field_name?: string; - detector_description?: string; - detector_index?: number; - exclude_frequent?: string; - field_name?: string; - function: string; - over_field_name?: string; - partition_field_name?: string; - use_null?: boolean; - custom_rules?: CustomRule[]; -} -export interface AnalysisLimits { - categorization_examples_limit?: number; - model_memory_limit: string; -} +export type Detector = estypes.Detector; +// export interface Detector { +// by_field_name?: string; +// detector_description?: string; +// detector_index?: number; +// exclude_frequent?: string; +// field_name?: string; +// function: string; +// over_field_name?: string; +// partition_field_name?: string; +// use_null?: boolean; +// custom_rules?: CustomRule[]; +// } -export interface DataDescription { - format?: string; - time_field: string; - time_format?: string; -} +export type AnalysisLimits = estypes.AnalysisLimits; +// export interface AnalysisLimits { +// categorization_examples_limit?: number; +// model_memory_limit: string; +// } -export interface ModelPlotConfig { - enabled?: boolean; - annotations_enabled?: boolean; - terms?: string; -} +export type DataDescription = estypes.DataDescription; +// export interface DataDescription { +// format?: string; +// time_field: string; +// time_format?: string; +// } +export type ModelPlotConfig = estypes.ModelPlotConfig; +// export interface ModelPlotConfig { +// enabled?: boolean; +// annotations_enabled?: boolean; +// terms?: string; +// } + +export type CustomRule = estypes.DetectionRule; // TODO, finish this when it's needed -export interface CustomRule { - actions: string[]; - scope?: object; - conditions: any[]; -} +// export interface CustomRule { +// actions: string[]; +// scope?: object; +// conditions: any[]; +// } export interface PerPartitionCategorization { enabled?: boolean; diff --git a/x-pack/plugins/ml/common/types/fields.ts b/x-pack/plugins/ml/common/types/fields.ts index 047852534965c2..f9f7f8fc7ead69 100644 --- a/x-pack/plugins/ml/common/types/fields.ts +++ b/x-pack/plugins/ml/common/types/fields.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; import { ES_FIELD_TYPES } from '../../../../../src/plugins/data/common'; import { ML_JOB_AGGREGATION, @@ -120,4 +121,4 @@ export interface RuntimeField { }; } -export type RuntimeMappings = Record; +export type RuntimeMappings = estypes.RuntimeFields; diff --git a/x-pack/plugins/ml/common/util/job_utils.ts b/x-pack/plugins/ml/common/util/job_utils.ts index 4b80661f13c093..10f5fb975ef5e1 100644 --- a/x-pack/plugins/ml/common/util/job_utils.ts +++ b/x-pack/plugins/ml/common/util/job_utils.ts @@ -733,7 +733,7 @@ export function validateGroupNames(job: Job): ValidationResults { * @return {Duration} the parsed interval, or null if it does not represent a valid * time interval. */ -export function parseTimeIntervalForJob(value: string | undefined): Duration | null { +export function parseTimeIntervalForJob(value: string | number | undefined): Duration | null { if (value === undefined) { return null; } @@ -748,7 +748,7 @@ export function parseTimeIntervalForJob(value: string | undefined): Duration | n // Checks that the value for a field which represents a time interval, // such as a job bucket span or datafeed query delay, is valid. -function isValidTimeInterval(value: string | undefined): boolean { +function isValidTimeInterval(value: string | number | undefined): boolean { if (value === undefined) { return true; } diff --git a/x-pack/plugins/ml/common/util/parse_interval.ts b/x-pack/plugins/ml/common/util/parse_interval.ts index c3013ef447792d..6ca280dc12ebda 100644 --- a/x-pack/plugins/ml/common/util/parse_interval.ts +++ b/x-pack/plugins/ml/common/util/parse_interval.ts @@ -34,7 +34,10 @@ const SUPPORT_ES_DURATION_UNITS: SupportedUnits[] = ['ms', 's', 'm', 'h', 'd']; // to work with units less than 'day'. // 3. Fractional intervals e.g. 1.5h or 4.5d are not allowed, in line with the behaviour // of the Elasticsearch date histogram aggregation. -export function parseInterval(interval: string, checkValidEsUnit = false): Duration | null { +export function parseInterval( + interval: string | number, + checkValidEsUnit = false +): Duration | null { const matches = String(interval).trim().match(INTERVAL_STRING_RE); if (!Array.isArray(matches) || matches.length < 3) { return null; diff --git a/x-pack/plugins/ml/public/application/components/data_grid/common.ts b/x-pack/plugins/ml/public/application/components/data_grid/common.ts index 69750b0ab1aaa7..312776f0d6a078 100644 --- a/x-pack/plugins/ml/public/application/components/data_grid/common.ts +++ b/x-pack/plugins/ml/public/application/components/data_grid/common.ts @@ -110,6 +110,7 @@ export const getRuntimeFieldsMapping = ( if (isPopulatedObject(ipRuntimeMappings)) { indexPatternFields.forEach((ipField) => { if (ipRuntimeMappings.hasOwnProperty(ipField)) { + // @ts-expect-error combinedRuntimeMappings[ipField] = ipRuntimeMappings[ipField]; } }); diff --git a/x-pack/plugins/ml/public/application/components/model_snapshots/revert_model_snapshot_flyout/chart_loader.ts b/x-pack/plugins/ml/public/application/components/model_snapshots/revert_model_snapshot_flyout/chart_loader.ts index a3753c8f000ae9..4788254e97d1e8 100644 --- a/x-pack/plugins/ml/public/application/components/model_snapshots/revert_model_snapshot_flyout/chart_loader.ts +++ b/x-pack/plugins/ml/public/application/components/model_snapshots/revert_model_snapshot_flyout/chart_loader.ts @@ -30,6 +30,7 @@ export function chartLoaderProvider(mlResultsService: MlResultsService) { job.data_counts.earliest_record_timestamp, job.data_counts.latest_record_timestamp, intervalMs, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options job.datafeed_config.indices_options ); if (resp.error !== undefined) { diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/advanced_job_creator.ts b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/advanced_job_creator.ts index 2ca95a14fb8124..aac36f3e4f573f 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/advanced_job_creator.ts +++ b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/advanced_job_creator.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; import { SavedSearchSavedObject } from '../../../../../../common/types/kibana'; import { JobCreator } from './job_creator'; @@ -28,7 +29,7 @@ export interface RichDetector { byField: SplitField; overField: SplitField; partitionField: SplitField; - excludeFrequent: string | null; + excludeFrequent: estypes.ExcludeFrequent | null; description: string | null; customRules: CustomRule[] | null; } @@ -56,7 +57,7 @@ export class AdvancedJobCreator extends JobCreator { byField: SplitField, overField: SplitField, partitionField: SplitField, - excludeFrequent: string | null, + excludeFrequent: estypes.ExcludeFrequent | null, description: string | null ) { // addDetector doesn't support adding new custom rules. @@ -83,7 +84,7 @@ export class AdvancedJobCreator extends JobCreator { byField: SplitField, overField: SplitField, partitionField: SplitField, - excludeFrequent: string | null, + excludeFrequent: estypes.ExcludeFrequent | null, description: string | null, index: number ) { @@ -114,7 +115,7 @@ export class AdvancedJobCreator extends JobCreator { byField: SplitField, overField: SplitField, partitionField: SplitField, - excludeFrequent: string | null, + excludeFrequent: estypes.ExcludeFrequent | null, description: string | null, customRules: CustomRule[] | null ): { detector: Detector; richDetector: RichDetector } { @@ -182,6 +183,7 @@ export class AdvancedJobCreator extends JobCreator { timeFieldName: this.timeFieldName, query: this.query, runtimeMappings: this.datafeedConfig.runtime_mappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options indicesOptions: this.datafeedConfig.indices_options, }); this.setTimeRange(start.epoch, end.epoch); diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts index ec5cb59964ffd5..13d46faaf21cf6 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts +++ b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts @@ -246,12 +246,20 @@ export class JobCreator { private _initModelPlotConfig() { // initialize configs to false if they are missing if (this._job_config.model_plot_config === undefined) { - this._job_config.model_plot_config = {}; + this._job_config.model_plot_config = { + enabled: false, + }; } - if (this._job_config.model_plot_config.enabled === undefined) { + if ( + this._job_config.model_plot_config !== undefined && + this._job_config.model_plot_config.enabled === undefined + ) { this._job_config.model_plot_config.enabled = false; } - if (this._job_config.model_plot_config.annotations_enabled === undefined) { + if ( + this._job_config.model_plot_config !== undefined && + this._job_config.model_plot_config.annotations_enabled === undefined + ) { this._job_config.model_plot_config.annotations_enabled = false; } } @@ -636,6 +644,7 @@ export class JobCreator { this._job_config.custom_settings !== undefined && this._job_config.custom_settings[setting] !== undefined ) { + // @ts-expect-error return this._job_config.custom_settings[setting]; } return null; @@ -710,6 +719,7 @@ export class JobCreator { this._datafeed_config.runtime_mappings = {}; } Object.entries(runtimeFieldMap).forEach(([key, val]) => { + // @ts-expect-error this._datafeed_config.runtime_mappings![key] = val; }); } diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/util/default_configs.ts b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/util/default_configs.ts index 201a304fd33560..bf354b8ad984f9 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/util/default_configs.ts +++ b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/util/default_configs.ts @@ -11,6 +11,7 @@ import { Job, Datafeed, Detector } from '../../../../../../../common/types/anoma import { splitIndexPatternNames } from '../../../../../../../common/util/job_utils'; export function createEmptyJob(): Job { + // @ts-expect-error return { job_id: '', description: '', @@ -27,6 +28,7 @@ export function createEmptyJob(): Job { } export function createEmptyDatafeed(indexPatternTitle: IndexPatternTitle): Datafeed { + // @ts-expect-error return { datafeed_id: '', job_id: '', diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/util/filter_runtime_mappings.test.ts b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/util/filter_runtime_mappings.test.ts index 43e7d4e45b6e0c..c67a93c5e06262 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/util/filter_runtime_mappings.test.ts +++ b/x-pack/plugins/ml/public/application/jobs/new_job/common/job_creator/util/filter_runtime_mappings.test.ts @@ -9,6 +9,7 @@ import { Job, Datafeed } from '../../../../../../../common/types/anomaly_detecti import { filterRuntimeMappings } from './filter_runtime_mappings'; function getJob(): Job { + // @ts-expect-error return { job_id: 'test', description: '', @@ -53,12 +54,14 @@ function getDatafeed(): Datafeed { runtime_mappings: { responsetime_big: { type: 'double', + // @ts-expect-error @elastic/elasticsearch StoredScript.language is required script: { source: "emit(doc['responsetime'].value * 100.0)", }, }, airline_lower: { type: 'keyword', + // @ts-expect-error @elastic/elasticsearch StoredScript.language is required script: { source: "emit(doc['airline'].value.toLowerCase())", }, diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/common/results_loader/categorization_examples_loader.ts b/x-pack/plugins/ml/public/application/jobs/new_job/common/results_loader/categorization_examples_loader.ts index 641eda3dbf3e81..b51cd9b99792b9 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/common/results_loader/categorization_examples_loader.ts +++ b/x-pack/plugins/ml/public/application/jobs/new_job/common/results_loader/categorization_examples_loader.ts @@ -52,6 +52,7 @@ export class CategorizationExamplesLoader { this._jobCreator.end, analyzer, this._jobCreator.runtimeMappings ?? undefined, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options this._jobCreator.datafeedConfig.indices_options ); return resp; diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/common/results_loader/results_loader.ts b/x-pack/plugins/ml/public/application/jobs/new_job/common/results_loader/results_loader.ts index a01581f7526c5a..6d5fa26af70241 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/common/results_loader/results_loader.ts +++ b/x-pack/plugins/ml/public/application/jobs/new_job/common/results_loader/results_loader.ts @@ -257,6 +257,7 @@ export class ResultsLoader { const fieldValues = await this._chartLoader.loadFieldExampleValues( this._jobCreator.splitField, this._jobCreator.runtimeMappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options this._jobCreator.datafeedConfig.indices_options ); if (fieldValues.length > 0) { diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/advanced_detector_modal/advanced_detector_modal.tsx b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/advanced_detector_modal/advanced_detector_modal.tsx index 751f12e14f6b50..10c160f58ff771 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/advanced_detector_modal/advanced_detector_modal.tsx +++ b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/advanced_detector_modal/advanced_detector_modal.tsx @@ -5,6 +5,7 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; import React, { FC, Fragment, useState, useContext, useEffect } from 'react'; import { EuiComboBox, @@ -170,6 +171,7 @@ export const AdvancedDetectorModal: FC = ({ byField, overField, partitionField, + // @ts-expect-error excludeFrequent: excludeFrequentOption.label !== '' ? excludeFrequentOption.label : null, description: descriptionOption !== '' ? descriptionOption : null, customRules: null, @@ -343,7 +345,9 @@ function createFieldOption(field: Field | null): EuiComboBoxOptionOption { }; } -function createExcludeFrequentOption(excludeFrequent: string | null): EuiComboBoxOptionOption { +function createExcludeFrequentOption( + excludeFrequent: estypes.ExcludeFrequent | null +): EuiComboBoxOptionOption { if (excludeFrequent === null) { return emptyOption; } diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/bucket_span_estimator/estimate_bucket_span.ts b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/bucket_span_estimator/estimate_bucket_span.ts index 85083146c13789..113bde6fbf93d9 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/bucket_span_estimator/estimate_bucket_span.ts +++ b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/bucket_span_estimator/estimate_bucket_span.ts @@ -42,6 +42,7 @@ export function useEstimateBucketSpan() { splitField: undefined, timeField: mlContext.currentIndexPattern.timeFieldName, runtimeMappings: jobCreator.runtimeMappings ?? undefined, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options indicesOptions: jobCreator.datafeedConfig.indices_options, }; diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/categorization_view/metric_selection_summary.tsx b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/categorization_view/metric_selection_summary.tsx index da9f306cf30e61..f3396a95738a6e 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/categorization_view/metric_selection_summary.tsx +++ b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/categorization_view/metric_selection_summary.tsx @@ -55,6 +55,7 @@ export const CategorizationDetectorsSummary: FC = () => { jobCreator.start, jobCreator.end, chartInterval.getInterval().asMilliseconds(), + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options jobCreator.datafeedConfig.indices_options ); setEventRateChartData(resp); diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/multi_metric_view/metric_selection.tsx b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/multi_metric_view/metric_selection.tsx index 46eb4b88d0518d..05154964690305 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/multi_metric_view/metric_selection.tsx +++ b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/multi_metric_view/metric_selection.tsx @@ -114,6 +114,7 @@ export const MultiMetricDetectors: FC = ({ setIsValid }) => { .loadFieldExampleValues( splitField, jobCreator.runtimeMappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options jobCreator.datafeedConfig.indices_options ) .then(setFieldValues) @@ -145,6 +146,7 @@ export const MultiMetricDetectors: FC = ({ setIsValid }) => { fieldValues.length > 0 ? fieldValues[0] : null, cs.intervalMs, jobCreator.runtimeMappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options jobCreator.datafeedConfig.indices_options ); setLineChartsData(resp); diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/multi_metric_view/metric_selection_summary.tsx b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/multi_metric_view/metric_selection_summary.tsx index a4c344d16482be..dc76fc01781123 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/multi_metric_view/metric_selection_summary.tsx +++ b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/multi_metric_view/metric_selection_summary.tsx @@ -44,6 +44,7 @@ export const MultiMetricDetectorsSummary: FC = () => { const tempFieldValues = await chartLoader.loadFieldExampleValues( jobCreator.splitField, jobCreator.runtimeMappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options jobCreator.datafeedConfig.indices_options ); setFieldValues(tempFieldValues); @@ -78,6 +79,7 @@ export const MultiMetricDetectorsSummary: FC = () => { fieldValues.length > 0 ? fieldValues[0] : null, cs.intervalMs, jobCreator.runtimeMappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options jobCreator.datafeedConfig.indices_options ); setLineChartsData(resp); diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/population_view/metric_selection.tsx b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/population_view/metric_selection.tsx index a7eaaff6111837..7f5a06925c7e8c 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/population_view/metric_selection.tsx +++ b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/population_view/metric_selection.tsx @@ -161,6 +161,7 @@ export const PopulationDetectors: FC = ({ setIsValid }) => { jobCreator.splitField, cs.intervalMs, jobCreator.runtimeMappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options jobCreator.datafeedConfig.indices_options ); @@ -184,6 +185,7 @@ export const PopulationDetectors: FC = ({ setIsValid }) => { fields: await chartLoader.loadFieldExampleValues( field, jobCreator.runtimeMappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options jobCreator.datafeedConfig.indices_options ), }; diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/population_view/metric_selection_summary.tsx b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/population_view/metric_selection_summary.tsx index 55a9d37d1115cc..31b436944a5b08 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/population_view/metric_selection_summary.tsx +++ b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/population_view/metric_selection_summary.tsx @@ -79,6 +79,7 @@ export const PopulationDetectorsSummary: FC = () => { jobCreator.splitField, cs.intervalMs, jobCreator.runtimeMappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options jobCreator.datafeedConfig.indices_options ); @@ -102,6 +103,7 @@ export const PopulationDetectorsSummary: FC = () => { fields: await chartLoader.loadFieldExampleValues( field, jobCreator.runtimeMappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options jobCreator.datafeedConfig.indices_options ), }; diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/single_metric_view/metric_selection.tsx b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/single_metric_view/metric_selection.tsx index 0e09a81908e838..c5c5cd4d8b7446 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/single_metric_view/metric_selection.tsx +++ b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/single_metric_view/metric_selection.tsx @@ -94,6 +94,7 @@ export const SingleMetricDetectors: FC = ({ setIsValid }) => { null, cs.intervalMs, jobCreator.runtimeMappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options jobCreator.datafeedConfig.indices_options ); if (resp[DTR_IDX] !== undefined) { diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/single_metric_view/metric_selection_summary.tsx b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/single_metric_view/metric_selection_summary.tsx index ced94b2095f722..5e64f4ef189845 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/single_metric_view/metric_selection_summary.tsx +++ b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/pick_fields_step/components/single_metric_view/metric_selection_summary.tsx @@ -60,6 +60,7 @@ export const SingleMetricDetectorsSummary: FC = () => { null, cs.intervalMs, jobCreator.runtimeMappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options jobCreator.datafeedConfig.indices_options ); if (resp[DTR_IDX] !== undefined) { diff --git a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/time_range_step/time_range.tsx b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/time_range_step/time_range.tsx index d2cf6b7a004713..b57fd45019abe0 100644 --- a/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/time_range_step/time_range.tsx +++ b/x-pack/plugins/ml/public/application/jobs/new_job/pages/components/time_range_step/time_range.tsx @@ -48,6 +48,7 @@ export const TimeRangeStep: FC = ({ setCurrentStep, isCurrentStep }) jobCreator.start, jobCreator.end, chartInterval.getInterval().asMilliseconds(), + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options jobCreator.datafeedConfig.indices_options ); setEventRateChartData(resp); diff --git a/x-pack/plugins/ml/public/application/util/string_utils.ts b/x-pack/plugins/ml/public/application/util/string_utils.ts index 9cd22d1d6ce76c..b981bbb8fe1a61 100644 --- a/x-pack/plugins/ml/public/application/util/string_utils.ts +++ b/x-pack/plugins/ml/public/application/util/string_utils.ts @@ -74,7 +74,7 @@ export function detectorToString(dtr: Detector): string { txt += PARTITION_FIELD_OPTION + quoteField(dtr.partition_field_name); } - if (dtr.exclude_frequent !== undefined && dtr.exclude_frequent !== '') { + if (dtr.exclude_frequent !== undefined) { txt += EXCLUDE_FREQUENT_OPTION + dtr.exclude_frequent; } diff --git a/x-pack/plugins/ml/server/lib/alerts/alerting_service.ts b/x-pack/plugins/ml/server/lib/alerts/alerting_service.ts index ce615418967210..81529669749bce 100644 --- a/x-pack/plugins/ml/server/lib/alerts/alerting_service.ts +++ b/x-pack/plugins/ml/server/lib/alerts/alerting_service.ts @@ -398,6 +398,7 @@ export function alertingServiceProvider(mlClient: MlClient, esClient: Elasticsea const response = await mlClient.anomalySearch( { + // @ts-expect-error body: requestBody, }, jobIds @@ -425,7 +426,8 @@ export function alertingServiceProvider(mlClient: MlClient, esClient: Elasticsea .filter((v) => v.doc_count > 0 && v[resultsLabel.aggGroupLabel].doc_count > 0) // Map response .map(formatter) - : [formatter(result as AggResultsResponse)] + : // @ts-expect-error + [formatter(result as AggResultsResponse)] ).filter(isDefined); }; diff --git a/x-pack/plugins/ml/server/lib/ml_client/ml_client.ts b/x-pack/plugins/ml/server/lib/ml_client/ml_client.ts index 278dd19f74acc3..6e76a536feb253 100644 --- a/x-pack/plugins/ml/server/lib/ml_client/ml_client.ts +++ b/x-pack/plugins/ml/server/lib/ml_client/ml_client.ts @@ -6,17 +6,10 @@ */ import { IScopedClusterClient } from 'kibana/server'; - import { JobSavedObjectService } from '../../saved_objects'; import { JobType } from '../../../common/types/saved_objects'; -import { - Job, - JobStats, - Datafeed, - DatafeedStats, -} from '../../../common/types/anomaly_detection_jobs'; -import { Calendar } from '../../../common/types/calendars'; +import { Job, Datafeed } from '../../../common/types/anomaly_detection_jobs'; import { searchProvider } from './search'; import { DataFrameAnalyticsConfig } from '../../../common/types/data_frame_analytics'; @@ -109,7 +102,7 @@ export function getMlClient( // similar to groupIdsCheck above, however we need to load the jobs first to get the groups information const ids = getADJobIdsFromRequest(p); if (ids.length) { - const { body } = await mlClient.getJobs<{ jobs: Job[] }>(...p); + const { body } = await mlClient.getJobs(...p); await groupIdsCheck(p, body.jobs, filteredJobIds); } } @@ -131,6 +124,7 @@ export function getMlClient( } } + // @ts-expect-error promise and TransportRequestPromise are incompatible. missing abort return { async closeJob(...p: Parameters) { await jobIdsCheck('anomaly-detector', p); @@ -152,7 +146,7 @@ export function getMlClient( // deleted initially and could still fail. return resp; }, - async deleteDatafeed(...p: any) { + async deleteDatafeed(...p: Parameters) { await datafeedIdsCheck(p); const resp = await mlClient.deleteDatafeed(...p); const [datafeedId] = getDatafeedIdsFromRequest(p); @@ -213,7 +207,7 @@ export function getMlClient( return mlClient.getCalendarEvents(...p); }, async getCalendars(...p: Parameters) { - const { body } = await mlClient.getCalendars<{ calendars: Calendar[] }, any>(...p); + const { body } = await mlClient.getCalendars(...p); const { body: { jobs: allJobs }, } = await mlClient.getJobs<{ jobs: Job[] }>(); @@ -263,9 +257,9 @@ export function getMlClient( // this should use DataFrameAnalyticsStats, but needs a refactor to move DataFrameAnalyticsStats to common await jobIdsCheck('data-frame-analytics', p, true); try { - const { body } = await mlClient.getDataFrameAnalyticsStats<{ - data_frame_analytics: DataFrameAnalyticsConfig[]; - }>(...p); + const { body } = ((await mlClient.getDataFrameAnalyticsStats(...p)) as unknown) as { + body: { data_frame_analytics: DataFrameAnalyticsConfig[] }; + }; const jobs = await jobSavedObjectService.filterJobsForSpace( 'data-frame-analytics', body.data_frame_analytics, @@ -282,8 +276,8 @@ export function getMlClient( async getDatafeedStats(...p: Parameters) { await datafeedIdsCheck(p, true); try { - const { body } = await mlClient.getDatafeedStats<{ datafeeds: DatafeedStats[] }>(...p); - const datafeeds = await jobSavedObjectService.filterDatafeedsForSpace( + const { body } = await mlClient.getDatafeedStats(...p); + const datafeeds = await jobSavedObjectService.filterDatafeedsForSpace( 'anomaly-detector', body.datafeeds, 'datafeed_id' @@ -299,7 +293,7 @@ export function getMlClient( async getDatafeeds(...p: Parameters) { await datafeedIdsCheck(p, true); try { - const { body } = await mlClient.getDatafeeds<{ datafeeds: Datafeed[] }>(...p); + const { body } = await mlClient.getDatafeeds(...p); const datafeeds = await jobSavedObjectService.filterDatafeedsForSpace( 'anomaly-detector', body.datafeeds, @@ -322,8 +316,8 @@ export function getMlClient( }, async getJobStats(...p: Parameters) { try { - const { body } = await mlClient.getJobStats<{ jobs: JobStats[] }>(...p); - const jobs = await jobSavedObjectService.filterJobsForSpace( + const { body } = await mlClient.getJobStats(...p); + const jobs = await jobSavedObjectService.filterJobsForSpace( 'anomaly-detector', body.jobs, 'job_id' diff --git a/x-pack/plugins/ml/server/lib/ml_client/search.ts b/x-pack/plugins/ml/server/lib/ml_client/search.ts index 158de0017fbbf0..3062a70d9a975a 100644 --- a/x-pack/plugins/ml/server/lib/ml_client/search.ts +++ b/x-pack/plugins/ml/server/lib/ml_client/search.ts @@ -7,11 +7,10 @@ import Boom from '@hapi/boom'; import { IScopedClusterClient } from 'kibana/server'; -import { RequestParams, ApiResponse } from '@elastic/elasticsearch'; +import { estypes, ApiResponse } from '@elastic/elasticsearch'; import { JobSavedObjectService } from '../../saved_objects'; import { ML_RESULTS_INDEX_PATTERN } from '../../../common/constants/index_patterns'; -import type { SearchResponse7 } from '../../../common/types/es_client'; import type { JobType } from '../../../common/types/saved_objects'; export function searchProvider( @@ -29,12 +28,12 @@ export function searchProvider( } async function anomalySearch( - searchParams: RequestParams.Search, + searchParams: estypes.SearchRequest, jobIds: string[] - ): Promise>> { + ): Promise>> { await jobIdsCheck('anomaly-detector', jobIds); const { asInternalUser } = client; - const resp = await asInternalUser.search>({ + const resp = await asInternalUser.search({ ...searchParams, index: ML_RESULTS_INDEX_PATTERN, }); diff --git a/x-pack/plugins/ml/server/lib/node_utils.ts b/x-pack/plugins/ml/server/lib/node_utils.ts index b76245fb9796cc..82e5d7f4698498 100644 --- a/x-pack/plugins/ml/server/lib/node_utils.ts +++ b/x-pack/plugins/ml/server/lib/node_utils.ts @@ -17,7 +17,7 @@ export async function getMlNodeCount(client: IScopedClusterClient): Promise { if (body.nodes[k].attributes !== undefined) { - const maxOpenJobs = body.nodes[k].attributes['ml.max_open_jobs']; + const maxOpenJobs = +body.nodes[k].attributes['ml.max_open_jobs']; if (maxOpenJobs !== null && maxOpenJobs > 0) { count++; } diff --git a/x-pack/plugins/ml/server/lib/query_utils.ts b/x-pack/plugins/ml/server/lib/query_utils.ts index 265962bb8432c7..dd4dc01498dbba 100644 --- a/x-pack/plugins/ml/server/lib/query_utils.ts +++ b/x-pack/plugins/ml/server/lib/query_utils.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; /* * Contains utility functions for building and processing queries. */ @@ -40,7 +41,10 @@ export function buildBaseFilterCriteria( // 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: object, samplerShardSize: number) { +export function buildSamplerAggregation( + aggs: any, + samplerShardSize: number +): Record { if (samplerShardSize < 1) { return aggs; } diff --git a/x-pack/plugins/ml/server/models/annotation_service/annotation.ts b/x-pack/plugins/ml/server/models/annotation_service/annotation.ts index f5c64cfa51efcc..2d5bd1f6f6e453 100644 --- a/x-pack/plugins/ml/server/models/annotation_service/annotation.ts +++ b/x-pack/plugins/ml/server/models/annotation_service/annotation.ts @@ -290,11 +290,13 @@ export function annotationProvider({ asInternalUser }: IScopedClusterClient) { try { const { body } = await asInternalUser.search(params); + // @ts-expect-error TODO fix search response types if (body.error !== undefined && body.message !== undefined) { // No need to translate, this will not be exposed in the UI. throw new Error(`Annotations couldn't be retrieved from Elasticsearch.`); } + // @ts-expect-error TODO fix search response types const docs: Annotations = get(body, ['hits', 'hits'], []).map((d: EsResult) => { // get the original source document and the document id, we need it // to identify the annotation when editing/deleting it. diff --git a/x-pack/plugins/ml/server/models/calculate_model_memory_limit/calculate_model_memory_limit.ts b/x-pack/plugins/ml/server/models/calculate_model_memory_limit/calculate_model_memory_limit.ts index 2efc2f905d9bb9..1f5bbe8ac0fd49 100644 --- a/x-pack/plugins/ml/server/models/calculate_model_memory_limit/calculate_model_memory_limit.ts +++ b/x-pack/plugins/ml/server/models/calculate_model_memory_limit/calculate_model_memory_limit.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import numeral from '@elastic/numeral'; import { IScopedClusterClient } from 'kibana/server'; import { MLCATEGORY } from '../../../common/constants/field_types'; @@ -89,12 +89,15 @@ const cardinalityCheckProvider = (client: IScopedClusterClient) => { new Set() ); - const maxBucketFieldCardinalities: string[] = influencers.filter( + const normalizedInfluencers: estypes.Field[] = Array.isArray(influencers) + ? influencers + : [influencers]; + const maxBucketFieldCardinalities = normalizedInfluencers.filter( (influencerField) => !!influencerField && !excludedKeywords.has(influencerField) && !overallCardinalityFields.has(influencerField) - ) as string[]; + ); if (overallCardinalityFields.size > 0) { overallCardinality = await fieldsService.getCardinalityOfFields( @@ -116,7 +119,7 @@ const cardinalityCheckProvider = (client: IScopedClusterClient) => { timeFieldName, earliestMs, latestMs, - bucketSpan, + bucketSpan as string, // update to Time type datafeedConfig ); } diff --git a/x-pack/plugins/ml/server/models/calendar/calendar_manager.ts b/x-pack/plugins/ml/server/models/calendar/calendar_manager.ts index 982485ab737aea..96bd74b9880a6f 100644 --- a/x-pack/plugins/ml/server/models/calendar/calendar_manager.ts +++ b/x-pack/plugins/ml/server/models/calendar/calendar_manager.ts @@ -5,14 +5,17 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; import { difference } from 'lodash'; -import { EventManager, CalendarEvent } from './event_manager'; +import { EventManager } from './event_manager'; import type { MlClient } from '../../lib/ml_client'; +type ScheduledEvent = estypes.ScheduledEvent; + interface BasicCalendar { job_ids: string[]; description?: string; - events: CalendarEvent[]; + events: ScheduledEvent[]; } export interface Calendar extends BasicCalendar { @@ -37,23 +40,24 @@ export class CalendarManager { calendar_id: calendarId, }); - const calendars = body.calendars; + const calendars = body.calendars as Calendar[]; const calendar = calendars[0]; // Endpoint throws a 404 if calendar is not found. calendar.events = await this._eventManager.getCalendarEvents(calendarId); return calendar; } async getAllCalendars() { + // @ts-expect-error missing size argument const { body } = await this._mlClient.getCalendars({ size: 1000 }); - const events: CalendarEvent[] = await this._eventManager.getAllEvents(); - const calendars: Calendar[] = body.calendars; + const events: ScheduledEvent[] = await this._eventManager.getAllEvents(); + const calendars: Calendar[] = body.calendars as Calendar[]; calendars.forEach((cal) => (cal.events = [])); // loop events and combine with related calendars events.forEach((event) => { const calendar = calendars.find((cal) => cal.calendar_id === event.calendar_id); - if (calendar) { + if (calendar && calendar.events) { calendar.events.push(event); } }); @@ -98,7 +102,7 @@ export class CalendarManager { ); // if an event in the original calendar cannot be found, it must have been deleted - const eventsToRemove: CalendarEvent[] = origCalendar.events.filter( + const eventsToRemove: ScheduledEvent[] = origCalendar.events.filter( (event) => calendar.events.find((e) => this._eventManager.isEqual(e, event)) === undefined ); diff --git a/x-pack/plugins/ml/server/models/calendar/event_manager.ts b/x-pack/plugins/ml/server/models/calendar/event_manager.ts index 42ad3b99184c6e..c870d67524135d 100644 --- a/x-pack/plugins/ml/server/models/calendar/event_manager.ts +++ b/x-pack/plugins/ml/server/models/calendar/event_manager.ts @@ -5,16 +5,11 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; import { GLOBAL_CALENDAR } from '../../../common/constants/calendars'; import type { MlClient } from '../../lib/ml_client'; -export interface CalendarEvent { - calendar_id?: string; - event_id?: string; - description: string; - start_time: number; - end_time: number; -} +type ScheduledEvent = estypes.ScheduledEvent; export class EventManager { private _mlClient: MlClient; @@ -39,7 +34,7 @@ export class EventManager { return body.events; } - async addEvents(calendarId: string, events: CalendarEvent[]) { + async addEvents(calendarId: string, events: ScheduledEvent[]) { const body = { events }; return await this._mlClient.postCalendarEvents({ @@ -55,7 +50,7 @@ export class EventManager { }); } - isEqual(ev1: CalendarEvent, ev2: CalendarEvent) { + isEqual(ev1: ScheduledEvent, ev2: ScheduledEvent) { return ( ev1.event_id === ev2.event_id && ev1.description === ev2.description && diff --git a/x-pack/plugins/ml/server/models/calendar/index.ts b/x-pack/plugins/ml/server/models/calendar/index.ts index 26fb1bbe2c2359..c5177dd675ca1c 100644 --- a/x-pack/plugins/ml/server/models/calendar/index.ts +++ b/x-pack/plugins/ml/server/models/calendar/index.ts @@ -6,4 +6,3 @@ */ export { CalendarManager, Calendar, FormCalendar } from './calendar_manager'; -export { CalendarEvent } from './event_manager'; diff --git a/x-pack/plugins/ml/server/models/data_frame_analytics/analytics_audit_messages.ts b/x-pack/plugins/ml/server/models/data_frame_analytics/analytics_audit_messages.ts index 0cbbf67dbbfac9..516823ff787582 100644 --- a/x-pack/plugins/ml/server/models/data_frame_analytics/analytics_audit_messages.ts +++ b/x-pack/plugins/ml/server/models/data_frame_analytics/analytics_audit_messages.ts @@ -79,9 +79,9 @@ export function analyticsAuditMessagesProvider({ asInternalUser }: IScopedCluste }, }); - let messages = []; - if (body.hits.total.value > 0) { - messages = body.hits.hits.map((hit: Message) => hit._source); + let messages: JobMessage[] = []; + if (typeof body.hits.total !== 'number' && body.hits.total.value > 0) { + messages = (body.hits.hits as Message[]).map((hit) => hit._source); messages.reverse(); } return messages; diff --git a/x-pack/plugins/ml/server/models/data_frame_analytics/validation.ts b/x-pack/plugins/ml/server/models/data_frame_analytics/validation.ts index 371446638814a5..4c79855f39e894 100644 --- a/x-pack/plugins/ml/server/models/data_frame_analytics/validation.ts +++ b/x-pack/plugins/ml/server/models/data_frame_analytics/validation.ts @@ -6,6 +6,7 @@ */ import { i18n } from '@kbn/i18n'; +import type { estypes } from '@elastic/elasticsearch'; import { IScopedClusterClient } from 'kibana/server'; import { getAnalysisType } from '../../../common/util/analytics_utils'; import { @@ -197,7 +198,7 @@ async function getValidationCheckMessages( analyzedFields: string[], index: string | string[], analysisConfig: AnalysisConfig, - query: unknown = defaultQuery + query: estypes.QueryContainer = defaultQuery ) { const analysisType = getAnalysisType(analysisConfig); const depVar = getDependentVar(analysisConfig); @@ -241,9 +242,11 @@ async function getValidationCheckMessages( }, }); + // @ts-expect-error const totalDocs = body.hits.total.value; if (body.aggregations) { + // @ts-expect-error Object.entries(body.aggregations).forEach(([aggName, { doc_count: docCount, value }]) => { const empty = docCount / totalDocs; diff --git a/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts b/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts index 4e99330610fca7..21ed258a0b764d 100644 --- a/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts +++ b/x-pack/plugins/ml/server/models/data_recognizer/data_recognizer.ts @@ -288,6 +288,7 @@ export class DataRecognizer { body: searchBody, }); + // @ts-expect-error fix search response return body.hits.total.value > 0; } @@ -864,10 +865,10 @@ export class DataRecognizer { try { const duration: { start: string; end?: string } = { start: '0' }; if (start !== undefined) { - duration.start = (start as unknown) as string; + duration.start = String(start); } if (end !== undefined) { - duration.end = (end as unknown) as string; + duration.end = String(end); } const { diff --git a/x-pack/plugins/ml/server/models/data_visualizer/data_visualizer.ts b/x-pack/plugins/ml/server/models/data_visualizer/data_visualizer.ts index 2a820e0629b757..679b7b3f12a23d 100644 --- a/x-pack/plugins/ml/server/models/data_visualizer/data_visualizer.ts +++ b/x-pack/plugins/ml/server/models/data_visualizer/data_visualizer.ts @@ -654,6 +654,7 @@ export class DataVisualizer { }); const aggregations = body.aggregations; + // @ts-expect-error fix search response const totalCount = body.hits.total.value; const stats = { totalCount, @@ -739,6 +740,7 @@ export class DataVisualizer { size, body: searchBody, }); + // @ts-expect-error fix search response return body.hits.total.value > 0; } @@ -1213,6 +1215,7 @@ export class DataVisualizer { fieldName: field, examples: [] as any[], }; + // @ts-expect-error fix search response if (body.hits.total.value > 0) { const hits = body.hits.hits; for (let i = 0; i < hits.length; i++) { diff --git a/x-pack/plugins/ml/server/models/fields_service/fields_service.ts b/x-pack/plugins/ml/server/models/fields_service/fields_service.ts index 8e4dbaf23212fe..c2b95d9a585849 100644 --- a/x-pack/plugins/ml/server/models/fields_service/fields_service.ts +++ b/x-pack/plugins/ml/server/models/fields_service/fields_service.ts @@ -184,6 +184,7 @@ export function fieldsServiceProvider({ asCurrentUser }: IScopedClusterClient) { } = await asCurrentUser.search({ index, body, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options ...(datafeedConfig?.indices_options ?? {}), }); @@ -192,6 +193,7 @@ export function fieldsServiceProvider({ asCurrentUser }: IScopedClusterClient) { } const aggResult = fieldsToAgg.reduce((obj, field) => { + // @ts-expect-error fix search aggregation response obj[field] = (aggregations[field] || { value: 0 }).value; return obj; }, {} as { [field: string]: number }); @@ -247,10 +249,14 @@ export function fieldsServiceProvider({ asCurrentUser }: IScopedClusterClient) { }); if (aggregations && aggregations.earliest && aggregations.latest) { + // @ts-expect-error fix search aggregation response obj.start.epoch = aggregations.earliest.value; + // @ts-expect-error fix search aggregation response obj.start.string = aggregations.earliest.value_as_string; + // @ts-expect-error fix search aggregation response obj.end.epoch = aggregations.latest.value; + // @ts-expect-error fix search aggregation response obj.end.string = aggregations.latest.value_as_string; } return obj; @@ -400,6 +406,7 @@ export function fieldsServiceProvider({ asCurrentUser }: IScopedClusterClient) { } = await asCurrentUser.search({ index, body, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options ...(datafeedConfig?.indices_options ?? {}), }); @@ -408,6 +415,7 @@ export function fieldsServiceProvider({ asCurrentUser }: IScopedClusterClient) { } const aggResult = fieldsToAgg.reduce((obj, field) => { + // @ts-expect-error fix search aggregation response obj[field] = (aggregations[getMaxBucketAggKey(field)] || { value: 0 }).value ?? 0; return obj; }, {} as { [field: string]: number }); diff --git a/x-pack/plugins/ml/server/models/filter/filter_manager.ts b/x-pack/plugins/ml/server/models/filter/filter_manager.ts index c0c8b53e7aac67..2a250872607955 100644 --- a/x-pack/plugins/ml/server/models/filter/filter_manager.ts +++ b/x-pack/plugins/ml/server/models/filter/filter_manager.ts @@ -5,10 +5,12 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; import Boom from '@hapi/boom'; import type { MlClient } from '../../lib/ml_client'; -import { DetectorRule, DetectorRuleScope } from '../../../common/types/detector_rules'; +// import { DetectorRule, DetectorRuleScope } from '../../../common/types/detector_rules'; +import { Job } from '../../../common/types/anomaly_detection_jobs'; export interface Filter { filter_id: string; @@ -46,17 +48,17 @@ interface FiltersInUse { [id: string]: FilterUsage; } -interface PartialDetector { - detector_description: string; - custom_rules: DetectorRule[]; -} +// interface PartialDetector { +// detector_description: string; +// custom_rules: DetectorRule[]; +// } -interface PartialJob { - job_id: string; - analysis_config: { - detectors: PartialDetector[]; - }; -} +// interface PartialJob { +// job_id: string; +// analysis_config: { +// detectors: PartialDetector[]; +// }; +// } export class FilterManager { constructor(private _mlClient: MlClient) {} @@ -69,15 +71,23 @@ export class FilterManager { this._mlClient.getFilters({ filter_id: filterId }), ]); - if (results[FILTERS] && results[FILTERS].body.filters.length) { + if ( + results[FILTERS] && + (results[FILTERS].body as estypes.GetFiltersResponse).filters.length + ) { let filtersInUse: FiltersInUse = {}; - if (results[JOBS] && results[JOBS].body.jobs) { - filtersInUse = this.buildFiltersInUse(results[JOBS].body.jobs); + if (results[JOBS] && (results[JOBS].body as estypes.GetJobsResponse).jobs) { + filtersInUse = this.buildFiltersInUse( + (results[JOBS].body as estypes.GetJobsResponse).jobs + ); } - const filter = results[FILTERS].body.filters[0]; - filter.used_by = filtersInUse[filter.filter_id]; - return filter; + const filter = (results[FILTERS].body as estypes.GetFiltersResponse).filters[0]; + return { + ...filter, + used_by: filtersInUse[filter.filter_id], + item_count: 0, + } as FilterStats; } else { throw Boom.notFound(`Filter with the id "${filterId}" not found`); } @@ -105,8 +115,8 @@ export class FilterManager { // Build a map of filter_ids against jobs and detectors using that filter. let filtersInUse: FiltersInUse = {}; - if (results[JOBS] && results[JOBS].body.jobs) { - filtersInUse = this.buildFiltersInUse(results[JOBS].body.jobs); + if (results[JOBS] && (results[JOBS].body as estypes.GetJobsResponse).jobs) { + filtersInUse = this.buildFiltersInUse((results[JOBS].body as estypes.GetJobsResponse).jobs); } // For each filter, return just @@ -115,8 +125,8 @@ export class FilterManager { // item_count // jobs using the filter const filterStats: FilterStats[] = []; - if (results[FILTERS] && results[FILTERS].body.filters) { - results[FILTERS].body.filters.forEach((filter: Filter) => { + if (results[FILTERS] && (results[FILTERS].body as estypes.GetFiltersResponse).filters) { + (results[FILTERS].body as estypes.GetFiltersResponse).filters.forEach((filter: Filter) => { const stats: FilterStats = { filter_id: filter.filter_id, description: filter.description, @@ -173,7 +183,7 @@ export class FilterManager { return body; } - buildFiltersInUse(jobsList: PartialJob[]) { + buildFiltersInUse(jobsList: Job[]) { // Build a map of filter_ids against jobs and detectors using that filter. const filtersInUse: FiltersInUse = {}; jobsList.forEach((job) => { @@ -183,7 +193,7 @@ export class FilterManager { const rules = detector.custom_rules; rules.forEach((rule) => { if (rule.scope) { - const ruleScope: DetectorRuleScope = rule.scope; + const ruleScope = rule.scope; const scopeFields = Object.keys(ruleScope); scopeFields.forEach((scopeField) => { const filter = ruleScope[scopeField]; diff --git a/x-pack/plugins/ml/server/models/job_service/datafeeds.ts b/x-pack/plugins/ml/server/models/job_service/datafeeds.ts index cf9b2027225c9c..8279571adbae27 100644 --- a/x-pack/plugins/ml/server/models/job_service/datafeeds.ts +++ b/x-pack/plugins/ml/server/models/job_service/datafeeds.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; import { i18n } from '@kbn/i18n'; import { IScopedClusterClient } from 'kibana/server'; import { JOB_STATE, DATAFEED_STATE } from '../../../common/constants/states'; @@ -27,7 +28,8 @@ export interface MlDatafeedsStatsResponse { interface Results { [id: string]: { - started: boolean; + started?: estypes.StartDatafeedResponse['started']; + stopped?: estypes.StopDatafeedResponse['stopped']; error?: any; }; } @@ -105,8 +107,10 @@ export function datafeedsProvider(client: IScopedClusterClient, mlClient: MlClie async function startDatafeed(datafeedId: string, start?: number, end?: number) { return mlClient.startDatafeed({ datafeed_id: datafeedId, - start: (start as unknown) as string, - end: (end as unknown) as string, + body: { + start: start !== undefined ? String(start) : undefined, + end: end !== undefined ? String(end) : undefined, + }, }); } @@ -115,18 +119,16 @@ export function datafeedsProvider(client: IScopedClusterClient, mlClient: MlClie for (const datafeedId of datafeedIds) { try { - const { body } = await mlClient.stopDatafeed<{ - started: boolean; - }>({ + const { body } = await mlClient.stopDatafeed({ datafeed_id: datafeedId, }); - results[datafeedId] = body; + results[datafeedId] = { stopped: body.stopped }; } catch (error) { if (isRequestTimeout(error)) { return fillResultsWithTimeouts(results, datafeedId, datafeedIds, DATAFEED_STATE.STOPPED); } else { results[datafeedId] = { - started: false, + stopped: false, error: error.body, }; } @@ -175,9 +177,7 @@ export function datafeedsProvider(client: IScopedClusterClient, mlClient: MlClie // get all the datafeeds and match it with the jobId const { body: { datafeeds }, - } = await mlClient.getDatafeeds( - excludeGenerated ? { exclude_generated: true } : {} - ); + } = await mlClient.getDatafeeds(excludeGenerated ? { exclude_generated: true } : {}); // for (const result of datafeeds) { if (result.job_id === jobId) { return result; @@ -190,7 +190,7 @@ export function datafeedsProvider(client: IScopedClusterClient, mlClient: MlClie try { const { body: { datafeeds: datafeedsResults }, - } = await mlClient.getDatafeeds({ + } = await mlClient.getDatafeeds({ datafeed_id: assumedDefaultDatafeedId, ...(excludeGenerated ? { exclude_generated: true } : {}), }); @@ -220,6 +220,7 @@ export function datafeedsProvider(client: IScopedClusterClient, mlClient: MlClie job.data_description.time_field, query, datafeed.runtime_mappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options datafeed.indices_options ); @@ -351,6 +352,7 @@ export function datafeedsProvider(client: IScopedClusterClient, mlClient: MlClie const data = { index: datafeed.indices, body, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options ...(datafeed.indices_options ?? {}), }; diff --git a/x-pack/plugins/ml/server/models/job_service/jobs.ts b/x-pack/plugins/ml/server/models/job_service/jobs.ts index ac3e00a918da81..d0d824a88f5a96 100644 --- a/x-pack/plugins/ml/server/models/job_service/jobs.ts +++ b/x-pack/plugins/ml/server/models/job_service/jobs.ts @@ -143,7 +143,10 @@ export function jobsProvider(client: IScopedClusterClient, mlClient: MlClient) { throw Boom.notFound(`Cannot find datafeed for job ${jobId}`); } - const { body } = await mlClient.stopDatafeed({ datafeed_id: datafeedId, force: true }); + const { body } = await mlClient.stopDatafeed({ + datafeed_id: datafeedId, + body: { force: true }, + }); if (body.stopped !== true) { return { success: false }; } @@ -316,6 +319,7 @@ export function jobsProvider(client: IScopedClusterClient, mlClient: MlClient) { (ds) => ds.datafeed_id === datafeed.datafeed_id ); if (datafeedStats) { + // @ts-expect-error datafeeds[datafeed.job_id] = { ...datafeed, ...datafeedStats }; } } @@ -384,6 +388,7 @@ export function jobsProvider(client: IScopedClusterClient, mlClient: MlClient) { if (jobStatsResults && jobStatsResults.jobs) { const jobStats = jobStatsResults.jobs.find((js) => js.job_id === tempJob.job_id); if (jobStats !== undefined) { + // @ts-expect-error tempJob = { ...tempJob, ...jobStats }; if (jobStats.node) { tempJob.node = jobStats.node; @@ -417,13 +422,20 @@ export function jobsProvider(client: IScopedClusterClient, mlClient: MlClient) { const detailed = true; const jobIds: string[] = []; try { - const { body } = await asInternalUser.tasks.list({ actions, detailed }); - Object.keys(body.nodes).forEach((nodeId) => { - const tasks = body.nodes[nodeId].tasks; - Object.keys(tasks).forEach((taskId) => { - jobIds.push(tasks[taskId].description.replace(/^delete-job-/, '')); - }); + const { body } = await asInternalUser.tasks.list({ + // @ts-expect-error @elastic-elasticsearch expects it to be a string + actions, + detailed, }); + + if (body.nodes) { + Object.keys(body.nodes).forEach((nodeId) => { + const tasks = body.nodes![nodeId].tasks; + Object.keys(tasks).forEach((taskId) => { + jobIds.push(tasks[taskId].description!.replace(/^delete-job-/, '')); + }); + }); + } } catch (e) { // if the user doesn't have permission to load the task list, // use the jobs list to get the ids of deleting jobs diff --git a/x-pack/plugins/ml/server/models/job_service/model_snapshots.ts b/x-pack/plugins/ml/server/models/job_service/model_snapshots.ts index f1f5d98b96a535..425dff89032a3f 100644 --- a/x-pack/plugins/ml/server/models/job_service/model_snapshots.ts +++ b/x-pack/plugins/ml/server/models/job_service/model_snapshots.ts @@ -73,8 +73,9 @@ export function modelSnapshotProvider(client: IScopedClusterClient, mlClient: Ml if (replay && model.snapshot_id === snapshotId && snapshot.model_snapshots.length) { // create calendar before starting restarting the datafeed if (calendarEvents !== undefined && calendarEvents.length) { + const calendarId = String(Date.now()); const calendar: FormCalendar = { - calendarId: String(Date.now()), + calendarId, job_ids: [jobId], description: i18n.translate( 'xpack.ml.models.jobService.revertModelSnapshot.autoCreatedCalendar.description', @@ -83,16 +84,18 @@ export function modelSnapshotProvider(client: IScopedClusterClient, mlClient: Ml } ), events: calendarEvents.map((s) => ({ + calendar_id: calendarId, + event_id: '', description: s.description, - start_time: s.start, - end_time: s.end, + start_time: `${s.start}`, + end_time: `${s.end}`, })), }; const cm = new CalendarManager(mlClient); await cm.newCalendar(calendar); } - forceStartDatafeeds([datafeedId], snapshot.model_snapshots[0].latest_record_time_stamp, end); + forceStartDatafeeds([datafeedId], +snapshot.model_snapshots[0].latest_record_time_stamp, end); } return { success: true }; diff --git a/x-pack/plugins/ml/server/models/job_service/new_job/categorization/examples.ts b/x-pack/plugins/ml/server/models/job_service/new_job/categorization/examples.ts index 37fa6753627732..b0ee20763f4305 100644 --- a/x-pack/plugins/ml/server/models/job_service/new_job/categorization/examples.ts +++ b/x-pack/plugins/ml/server/models/job_service/new_job/categorization/examples.ts @@ -127,7 +127,7 @@ export function categorizationExamplesProvider({ async function loadTokens(examples: string[], analyzer: CategorizationAnalyzer) { const { body: { tokens }, - } = await asInternalUser.indices.analyze<{ tokens: Token[] }>({ + } = await asInternalUser.indices.analyze({ body: { ...getAnalyzer(analyzer), text: examples, @@ -139,19 +139,21 @@ export function categorizationExamplesProvider({ const tokensPerExample: Token[][] = examples.map((e) => []); - tokens.forEach((t, i) => { - for (let g = 0; g < sumLengths.length; g++) { - if (t.start_offset <= sumLengths[g] + g) { - const offset = g > 0 ? sumLengths[g - 1] + g : 0; - tokensPerExample[g].push({ - ...t, - start_offset: t.start_offset - offset, - end_offset: t.end_offset - offset, - }); - break; + if (tokens !== undefined) { + tokens.forEach((t, i) => { + for (let g = 0; g < sumLengths.length; g++) { + if (t.start_offset <= sumLengths[g] + g) { + const offset = g > 0 ? sumLengths[g - 1] + g : 0; + tokensPerExample[g].push({ + ...t, + start_offset: t.start_offset - offset, + end_offset: t.end_offset - offset, + }); + break; + } } - } - }); + }); + } return tokensPerExample; } diff --git a/x-pack/plugins/ml/server/models/job_service/new_job/categorization/top_categories.ts b/x-pack/plugins/ml/server/models/job_service/new_job/categorization/top_categories.ts index 6637faeba094de..851336056a7f51 100644 --- a/x-pack/plugins/ml/server/models/job_service/new_job/categorization/top_categories.ts +++ b/x-pack/plugins/ml/server/models/job_service/new_job/categorization/top_categories.ts @@ -81,6 +81,7 @@ export function topCategoriesProvider(mlClient: MlClient) { const catCounts: Array<{ id: CategoryId; count: number; + // @ts-expect-error }> = body.aggregations?.cat_count?.buckets.map((c: any) => ({ id: c.key, count: c.doc_count, @@ -125,6 +126,7 @@ export function topCategoriesProvider(mlClient: MlClient) { [] ); + // @ts-expect-error return body.hits.hits?.map((c: { _source: Category }) => c._source) || []; } diff --git a/x-pack/plugins/ml/server/models/job_service/new_job_caps/field_service.ts b/x-pack/plugins/ml/server/models/job_service/new_job_caps/field_service.ts index 7ce54cd2f9c5e2..0287c2af11a7e7 100644 --- a/x-pack/plugins/ml/server/models/job_service/new_job_caps/field_service.ts +++ b/x-pack/plugins/ml/server/models/job_service/new_job_caps/field_service.ts @@ -5,13 +5,14 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; import { IScopedClusterClient } from 'kibana/server'; import { cloneDeep } from 'lodash'; import { SavedObjectsClientContract } from 'kibana/server'; import { Field, FieldId, NewJobCaps, RollupFields } from '../../../../common/types/fields'; import { ES_FIELD_TYPES } from '../../../../../../../src/plugins/data/common'; import { combineFieldsAndAggs } from '../../../../common/util/fields_utils'; -import { rollupServiceProvider, RollupJob } from './rollup'; +import { rollupServiceProvider } from './rollup'; import { aggregations, mlOnlyAggregations } from '../../../../common/constants/aggregation_types'; const supportedTypes: string[] = [ @@ -109,7 +110,9 @@ class FieldsService { this._mlClusterClient, this._savedObjectsClient ); - const rollupConfigs: RollupJob[] | null = await rollupService.getRollupJobs(); + const rollupConfigs: + | estypes.RollupCapabilitiesJob[] + | null = await rollupService.getRollupJobs(); // if a rollup index has been specified, yet there are no // rollup configs, return with no results @@ -131,14 +134,16 @@ class FieldsService { } } -function combineAllRollupFields(rollupConfigs: RollupJob[]): RollupFields { +function combineAllRollupFields(rollupConfigs: estypes.RollupCapabilitiesJob[]): RollupFields { const rollupFields: RollupFields = {}; rollupConfigs.forEach((conf) => { Object.keys(conf.fields).forEach((fieldName) => { if (rollupFields[fieldName] === undefined) { + // @ts-expect-error fix type. our RollupFields type is better rollupFields[fieldName] = conf.fields[fieldName]; } else { const aggs = conf.fields[fieldName]; + // @ts-expect-error fix type. our RollupFields type is better aggs.forEach((agg) => { if (rollupFields[fieldName].find((f) => f.agg === agg.agg) === null) { rollupFields[fieldName].push(agg); diff --git a/x-pack/plugins/ml/server/models/job_service/new_job_caps/rollup.ts b/x-pack/plugins/ml/server/models/job_service/new_job_caps/rollup.ts index 3b480bae2199eb..d83f7afb4cdf6c 100644 --- a/x-pack/plugins/ml/server/models/job_service/new_job_caps/rollup.ts +++ b/x-pack/plugins/ml/server/models/job_service/new_job_caps/rollup.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; import { IScopedClusterClient } from 'kibana/server'; import { SavedObject } from 'kibana/server'; import { IndexPatternAttributes } from 'src/plugins/data/server'; @@ -26,7 +27,7 @@ export async function rollupServiceProvider( const rollupIndexPatternObject = await loadRollupIndexPattern(indexPattern, savedObjectsClient); let jobIndexPatterns: string[] = [indexPattern]; - async function getRollupJobs(): Promise { + async function getRollupJobs(): Promise { if (rollupIndexPatternObject !== null) { const parsedTypeMetaData = JSON.parse(rollupIndexPatternObject.attributes.typeMeta); const rollUpIndex: string = parsedTypeMetaData.params.rollup_index; @@ -36,7 +37,7 @@ export async function rollupServiceProvider( const indexRollupCaps = rollupCaps[rollUpIndex]; if (indexRollupCaps && indexRollupCaps.rollup_jobs) { - jobIndexPatterns = indexRollupCaps.rollup_jobs.map((j: RollupJob) => j.index_pattern); + jobIndexPatterns = indexRollupCaps.rollup_jobs.map((j) => j.index_pattern); return indexRollupCaps.rollup_jobs; } diff --git a/x-pack/plugins/ml/server/models/job_validation/job_validation.ts b/x-pack/plugins/ml/server/models/job_validation/job_validation.ts index 2beade7f5dbc46..94e9a8dc7bffbb 100644 --- a/x-pack/plugins/ml/server/models/job_validation/job_validation.ts +++ b/x-pack/plugins/ml/server/models/job_validation/job_validation.ts @@ -69,6 +69,7 @@ export async function validateJob( timeField, job.datafeed_config.query, job.datafeed_config.runtime_mappings, + // @ts-expect-error @elastic/elasticsearch Datafeed is missing indices_options job.datafeed_config.indices_options ); diff --git a/x-pack/plugins/ml/server/models/job_validation/validate_model_memory_limit.test.ts b/x-pack/plugins/ml/server/models/job_validation/validate_model_memory_limit.test.ts index 853f96ad777434..44c5e3cabb18fa 100644 --- a/x-pack/plugins/ml/server/models/job_validation/validate_model_memory_limit.test.ts +++ b/x-pack/plugins/ml/server/models/job_validation/validate_model_memory_limit.test.ts @@ -148,8 +148,7 @@ describe('ML - validateModelMemoryLimit', () => { it('Called with no duration or split and mml above limit', () => { const job = getJobConfig(); const duration = undefined; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '31mb'; + job.analysis_limits!.model_memory_limit = '31mb'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -166,8 +165,7 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(10); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '20mb'; + job.analysis_limits!.model_memory_limit = '20mb'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -184,8 +182,7 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(2); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '30mb'; + job.analysis_limits!.model_memory_limit = '30mb'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -202,8 +199,7 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(2); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '10mb'; + job.analysis_limits!.model_memory_limit = '10mb'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -222,8 +218,7 @@ describe('ML - validateModelMemoryLimit', () => { const duration = { start: 0, end: 1 }; // @ts-expect-error delete mlInfoResponse.limits.max_model_memory_limit; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '10mb'; + job.analysis_limits!.model_memory_limit = '10mb'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -239,8 +234,7 @@ describe('ML - validateModelMemoryLimit', () => { it('Called with no duration or split and mml above limit, no max setting', () => { const job = getJobConfig(); const duration = undefined; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '31mb'; + job.analysis_limits!.model_memory_limit = '31mb'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -256,8 +250,7 @@ describe('ML - validateModelMemoryLimit', () => { it('Called with no duration or split and mml above limit, no max setting, above effective max mml', () => { const job = getJobConfig(); const duration = undefined; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '41mb'; + job.analysis_limits!.model_memory_limit = '41mb'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -274,8 +267,7 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '20mb'; + job.analysis_limits!.model_memory_limit = '20mb'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -292,8 +284,7 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '0mb'; + job.analysis_limits!.model_memory_limit = '0mb'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -310,8 +301,7 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '10mbananas'; + job.analysis_limits!.model_memory_limit = '10mbananas'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -328,8 +318,7 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '10'; + job.analysis_limits!.model_memory_limit = '10'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -346,8 +335,7 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-expect-error - job.analysis_limits.model_memory_limit = 'mb'; + job.analysis_limits!.model_memory_limit = 'mb'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -364,8 +352,7 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-expect-error - job.analysis_limits.model_memory_limit = 'asdf'; + job.analysis_limits!.model_memory_limit = 'asdf'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -382,8 +369,7 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '1023KB'; + job.analysis_limits!.model_memory_limit = '1023KB'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -400,8 +386,7 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '1024KB'; + job.analysis_limits!.model_memory_limit = '1024KB'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -418,8 +403,7 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '6MB'; + job.analysis_limits!.model_memory_limit = '6MB'; return validateModelMemoryLimit( getMockMlClusterClient(), @@ -436,8 +420,7 @@ describe('ML - validateModelMemoryLimit', () => { const dtrs = createDetectors(1); const job = getJobConfig(['instance'], dtrs); const duration = { start: 0, end: 1 }; - // @ts-expect-error - job.analysis_limits.model_memory_limit = '20MB'; + job.analysis_limits!.model_memory_limit = '20MB'; return validateModelMemoryLimit( getMockMlClusterClient(), diff --git a/x-pack/plugins/ml/server/models/results_service/get_partition_fields_values.ts b/x-pack/plugins/ml/server/models/results_service/get_partition_fields_values.ts index 81e26808bd2461..be1786d64f2aa6 100644 --- a/x-pack/plugins/ml/server/models/results_service/get_partition_fields_values.ts +++ b/x-pack/plugins/ml/server/models/results_service/get_partition_fields_values.ts @@ -10,7 +10,6 @@ import { PARTITION_FIELDS } from '../../../common/constants/anomalies'; import { PartitionFieldsType } from '../../../common/types/anomalies'; import { CriteriaField } from './results_service'; import { FieldConfig, FieldsConfig } from '../../routes/schemas/results_service_schema'; -import { Job } from '../../../common/types/anomaly_detection_jobs'; import type { MlClient } from '../../lib/ml_client'; type SearchTerm = @@ -151,7 +150,7 @@ export const getPartitionFieldsValuesFactory = (mlClient: MlClient) => throw Boom.notFound(`Job with the id "${jobId}" not found`); } - const job: Job = jobsResponse.jobs[0]; + const job = jobsResponse.jobs[0]; const isModelPlotEnabled = job?.model_plot_config?.enabled; const isAnomalousOnly = (Object.entries(fieldsConfig) as Array<[string, FieldConfig]>).some( diff --git a/x-pack/plugins/ml/server/models/results_service/results_service.ts b/x-pack/plugins/ml/server/models/results_service/results_service.ts index 3d1db66cc24cc1..1996acd2cdb066 100644 --- a/x-pack/plugins/ml/server/models/results_service/results_service.ts +++ b/x-pack/plugins/ml/server/models/results_service/results_service.ts @@ -183,6 +183,7 @@ export function resultsServiceProvider(mlClient: MlClient) { anomalies: [], interval: 'second', }; + // @ts-expect-error update to correct search response if (body.hits.total.value > 0) { let records: AnomalyRecordDoc[] = []; body.hits.hits.forEach((hit: any) => { @@ -401,6 +402,7 @@ export function resultsServiceProvider(mlClient: MlClient) { ); const examplesByCategoryId: { [key: string]: any } = {}; + // @ts-expect-error update to correct search response if (body.hits.total.value > 0) { body.hits.hits.forEach((hit: any) => { if (maxExamples) { @@ -437,6 +439,7 @@ export function resultsServiceProvider(mlClient: MlClient) { ); const definition = { categoryId, terms: null, regex: null, examples: [] }; + // @ts-expect-error update to correct search response if (body.hits.total.value > 0) { const source = body.hits.hits[0]._source; definition.categoryId = source.category_id; @@ -576,6 +579,7 @@ export function resultsServiceProvider(mlClient: MlClient) { ); if (fieldToBucket === JOB_ID) { finalResults = { + // @ts-expect-error update search response jobs: results.aggregations?.unique_terms?.buckets.map( (b: { key: string; doc_count: number }) => b.key ), @@ -588,6 +592,7 @@ export function resultsServiceProvider(mlClient: MlClient) { }, {} ); + // @ts-expect-error update search response results.aggregations.jobs.buckets.forEach( (bucket: { key: string | number; unique_stopped_partitions: { buckets: any[] } }) => { jobs[bucket.key] = bucket.unique_stopped_partitions.buckets.map((b) => b.key); diff --git a/x-pack/plugins/ml/server/routes/anomaly_detectors.ts b/x-pack/plugins/ml/server/routes/anomaly_detectors.ts index aa7adbe2db6605..ed583bd9e3eb18 100644 --- a/x-pack/plugins/ml/server/routes/anomaly_detectors.ts +++ b/x-pack/plugins/ml/server/routes/anomaly_detectors.ts @@ -5,8 +5,8 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; import { schema } from '@kbn/config-schema'; -import { RequestParams } from '@elastic/elasticsearch'; import { wrapError } from '../client/error_wrapper'; import { RouteInitialization } from '../types'; import { @@ -23,8 +23,6 @@ import { updateModelSnapshotSchema, } from './schemas/anomaly_detectors_schema'; -import { Job, JobStats } from '../../common/types/anomaly_detection_jobs'; - /** * Routes for the anomaly detectors */ @@ -49,7 +47,7 @@ export function jobRoutes({ router, routeGuard }: RouteInitialization) { }, routeGuard.fullLicenseAPIGuard(async ({ mlClient, response }) => { try { - const { body } = await mlClient.getJobs<{ jobs: Job[] }>(); + const { body } = await mlClient.getJobs(); return response.ok({ body, }); @@ -81,7 +79,7 @@ export function jobRoutes({ router, routeGuard }: RouteInitialization) { routeGuard.fullLicenseAPIGuard(async ({ mlClient, request, response }) => { try { const { jobId } = request.params; - const { body } = await mlClient.getJobs<{ jobs: Job[] }>({ job_id: jobId }); + const { body } = await mlClient.getJobs({ job_id: jobId }); return response.ok({ body, }); @@ -111,7 +109,7 @@ export function jobRoutes({ router, routeGuard }: RouteInitialization) { }, routeGuard.fullLicenseAPIGuard(async ({ mlClient, response }) => { try { - const { body } = await mlClient.getJobStats<{ jobs: JobStats[] }>(); + const { body } = await mlClient.getJobStats(); return response.ok({ body, }); @@ -283,7 +281,7 @@ export function jobRoutes({ router, routeGuard }: RouteInitialization) { }, routeGuard.fullLicenseAPIGuard(async ({ mlClient, request, response }) => { try { - const options: RequestParams.MlCloseJob = { + const options: estypes.CloseJobRequest = { job_id: request.params.jobId, }; const force = request.query.force; @@ -321,7 +319,7 @@ export function jobRoutes({ router, routeGuard }: RouteInitialization) { }, routeGuard.fullLicenseAPIGuard(async ({ mlClient, request, response }) => { try { - const options: RequestParams.MlDeleteJob = { + const options: estypes.DeleteJobRequest = { job_id: request.params.jobId, wait_for_completion: false, }; @@ -395,7 +393,9 @@ export function jobRoutes({ router, routeGuard }: RouteInitialization) { const duration = request.body.duration; const { body } = await mlClient.forecast({ job_id: jobId, - duration, + body: { + duration, + }, }); return response.ok({ body, @@ -513,10 +513,12 @@ export function jobRoutes({ router, routeGuard }: RouteInitialization) { try { const { body } = await mlClient.getOverallBuckets({ job_id: request.params.jobId, - top_n: request.body.topN, - bucket_span: request.body.bucketSpan, - start: request.body.start, - end: request.body.end, + body: { + top_n: request.body.topN, + bucket_span: request.body.bucketSpan, + start: request.body.start !== undefined ? String(request.body.start) : undefined, + end: request.body.end !== undefined ? String(request.body.end) : undefined, + }, }); return response.ok({ body, diff --git a/x-pack/plugins/ml/server/routes/data_frame_analytics.ts b/x-pack/plugins/ml/server/routes/data_frame_analytics.ts index 448d798845e188..520f8ce6fb0a98 100644 --- a/x-pack/plugins/ml/server/routes/data_frame_analytics.ts +++ b/x-pack/plugins/ml/server/routes/data_frame_analytics.ts @@ -28,7 +28,6 @@ import { AnalyticsManager } from '../models/data_frame_analytics/analytics_manag import { validateAnalyticsJob } from '../models/data_frame_analytics/validation'; import { DeleteDataFrameAnalyticsWithIndexStatus } from '../../common/types/data_frame_analytics'; import { getAuthorizationHeader } from '../lib/request_authorization'; -import { DataFrameAnalyticsConfig } from '../../common/types/data_frame_analytics'; import type { MlClient } from '../lib/ml_client'; function getIndexPatternId(context: RequestHandlerContext, patternName: string) { @@ -603,14 +602,10 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense, routeGuard }: Rout for (const id of analyticsIds) { try { const { body } = allSpaces - ? await client.asInternalUser.ml.getDataFrameAnalytics<{ - data_frame_analytics: DataFrameAnalyticsConfig[]; - }>({ + ? await client.asInternalUser.ml.getDataFrameAnalytics({ id, }) - : await mlClient.getDataFrameAnalytics<{ - data_frame_analytics: DataFrameAnalyticsConfig[]; - }>({ + : await mlClient.getDataFrameAnalytics({ id, }); results[id] = body.data_frame_analytics.length > 0; diff --git a/x-pack/plugins/ml/server/routes/datafeeds.ts b/x-pack/plugins/ml/server/routes/datafeeds.ts index 90d90a0e2b1e4d..2013af3ee87352 100644 --- a/x-pack/plugins/ml/server/routes/datafeeds.ts +++ b/x-pack/plugins/ml/server/routes/datafeeds.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { RequestParams } from '@elastic/elasticsearch'; +import { estypes } from '@elastic/elasticsearch'; import { wrapError } from '../client/error_wrapper'; import { RouteInitialization } from '../types'; import { @@ -16,8 +16,6 @@ import { } from './schemas/datafeeds_schema'; import { getAuthorizationHeader } from '../lib/request_authorization'; -import { Datafeed, DatafeedStats } from '../../common/types/anomaly_detection_jobs'; - /** * Routes for datafeed service */ @@ -39,7 +37,7 @@ export function dataFeedRoutes({ router, routeGuard }: RouteInitialization) { }, routeGuard.fullLicenseAPIGuard(async ({ mlClient, response }) => { try { - const { body } = await mlClient.getDatafeeds<{ datafeeds: Datafeed[] }>(); + const { body } = await mlClient.getDatafeeds(); return response.ok({ body, }); @@ -99,9 +97,7 @@ export function dataFeedRoutes({ router, routeGuard }: RouteInitialization) { }, routeGuard.fullLicenseAPIGuard(async ({ mlClient, response }) => { try { - const { body } = await mlClient.getDatafeedStats<{ - datafeeds: DatafeedStats[]; - }>(); + const { body } = await mlClient.getDatafeedStats(); return response.ok({ body, }); @@ -251,7 +247,7 @@ export function dataFeedRoutes({ router, routeGuard }: RouteInitialization) { }, routeGuard.fullLicenseAPIGuard(async ({ mlClient, request, response }) => { try { - const options: RequestParams.MlDeleteDatafeed = { + const options: estypes.DeleteDatafeedRequest = { datafeed_id: request.params.datafeedId, }; const force = request.query.force; @@ -298,8 +294,10 @@ export function dataFeedRoutes({ router, routeGuard }: RouteInitialization) { const { body } = await mlClient.startDatafeed({ datafeed_id: datafeedId, - start, - end, + body: { + start: start !== undefined ? String(start) : undefined, + end: end !== undefined ? String(end) : undefined, + }, }); return response.ok({ diff --git a/x-pack/plugins/ml/server/routes/trained_models.ts b/x-pack/plugins/ml/server/routes/trained_models.ts index bfa7137b3e6d6b..dbfc2195a12e1e 100644 --- a/x-pack/plugins/ml/server/routes/trained_models.ts +++ b/x-pack/plugins/ml/server/routes/trained_models.ts @@ -13,7 +13,6 @@ import { optionalModelIdSchema, } from './schemas/inference_schema'; import { modelsProvider } from '../models/data_frame_analytics'; -import { InferenceConfigResponse } from '../../common/types/trained_models'; export function trainedModelsRoutes({ router, routeGuard }: RouteInitialization) { /** @@ -38,7 +37,7 @@ export function trainedModelsRoutes({ router, routeGuard }: RouteInitialization) try { const { modelId } = request.params; const { with_pipelines: withPipelines, ...query } = request.query; - const { body } = await mlClient.getTrainedModels({ + const { body } = await mlClient.getTrainedModels({ size: 1000, ...query, ...(modelId ? { model_id: modelId } : {}), @@ -85,7 +84,7 @@ export function trainedModelsRoutes({ router, routeGuard }: RouteInitialization) tags: ['access:ml:canGetDataFrameAnalytics'], }, }, - routeGuard.fullLicenseAPIGuard(async ({ client, mlClient, request, response }) => { + routeGuard.fullLicenseAPIGuard(async ({ mlClient, request, response }) => { try { const { modelId } = request.params; const { body } = await mlClient.getTrainedModelsStats({ diff --git a/x-pack/plugins/ml/server/saved_objects/checks.ts b/x-pack/plugins/ml/server/saved_objects/checks.ts index 04d5e53a5568ba..6b24ef000b6951 100644 --- a/x-pack/plugins/ml/server/saved_objects/checks.ts +++ b/x-pack/plugins/ml/server/saved_objects/checks.ts @@ -10,8 +10,6 @@ import { IScopedClusterClient, KibanaRequest } from 'kibana/server'; import type { JobSavedObjectService } from './service'; import { JobType, DeleteJobCheckResponse } from '../../common/types/saved_objects'; -import { Job } from '../../common/types/anomaly_detection_jobs'; -import { Datafeed } from '../../common/types/anomaly_detection_jobs'; import { DataFrameAnalyticsConfig } from '../../common/types/data_frame_analytics'; import { ResolveMlCapabilities } from '../../common/types/capabilities'; @@ -51,13 +49,13 @@ export function checksFactory( const jobObjects = await jobSavedObjectService.getAllJobObjects(undefined, false); // load all non-space jobs and datafeeds - const { body: adJobs } = await client.asInternalUser.ml.getJobs<{ jobs: Job[] }>(); - const { body: datafeeds } = await client.asInternalUser.ml.getDatafeeds<{ - datafeeds: Datafeed[]; - }>(); - const { body: dfaJobs } = await client.asInternalUser.ml.getDataFrameAnalytics<{ - data_frame_analytics: DataFrameAnalyticsConfig[]; - }>(); + const { body: adJobs } = await client.asInternalUser.ml.getJobs(); + const { body: datafeeds } = await client.asInternalUser.ml.getDatafeeds(); + const { + body: dfaJobs, + } = ((await client.asInternalUser.ml.getDataFrameAnalytics()) as unknown) as { + body: { data_frame_analytics: DataFrameAnalyticsConfig[] }; + }; const savedObjectsStatus: JobSavedObjectStatus[] = jobObjects.map( ({ attributes, namespaces }) => { diff --git a/x-pack/plugins/ml/server/shared_services/providers/system.ts b/x-pack/plugins/ml/server/shared_services/providers/system.ts index 021bf5b12625ba..1e3dcd7de52408 100644 --- a/x-pack/plugins/ml/server/shared_services/providers/system.ts +++ b/x-pack/plugins/ml/server/shared_services/providers/system.ts @@ -7,7 +7,6 @@ import { KibanaRequest, SavedObjectsClientContract } from 'kibana/server'; import { SearchResponse } from 'elasticsearch'; -import { RequestParams } from '@elastic/elasticsearch'; import { MlLicense } from '../../../common/license'; import { CloudSetup } from '../../../../cloud/server'; import { spacesUtilsProvider } from '../../lib/spaces_utils'; @@ -24,10 +23,7 @@ export interface MlSystemProvider { ): { mlCapabilities(): Promise; mlInfo(): Promise; - mlAnomalySearch( - searchParams: RequestParams.Search, - jobIds: string[] - ): Promise>; + mlAnomalySearch(searchParams: any, jobIds: string[]): Promise>; }; } @@ -73,10 +69,7 @@ export function getMlSystemProvider( }; }); }, - async mlAnomalySearch( - searchParams: RequestParams.Search, - jobIds: string[] - ): Promise> { + async mlAnomalySearch(searchParams: any, jobIds: string[]): Promise> { return await getGuards(request, savedObjectsClient) .isFullLicense() .hasMlCapabilities(['canAccessML']) diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_available_ccs.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_available_ccs.test.ts index ecfb5fc50a16d6..603c66d2d05f2b 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_available_ccs.test.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_available_ccs.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { elasticsearchClientMock } from 'src/core/server/elasticsearch/client/mocks'; import { elasticsearchServiceMock } from 'src/core/server/mocks'; @@ -25,7 +26,7 @@ describe('fetchAvailableCcs', () => { elasticsearchClientMock.createSuccessTransportRequestPromise({ [connectedRemote]: { connected: true, - }, + } as estypes.RemoteInfo, }) ); @@ -40,7 +41,7 @@ describe('fetchAvailableCcs', () => { elasticsearchClientMock.createSuccessTransportRequestPromise({ [disconnectedRemote]: { connected: false, - }, + } as estypes.RemoteInfo, }) ); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_ccr_read_exceptions.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_ccr_read_exceptions.ts index 330be4e90ed567..fb67cd18059501 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_ccr_read_exceptions.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_ccr_read_exceptions.ts @@ -69,8 +69,8 @@ export async function fetchCCRReadExceptions( sort: [ { timestamp: { - order: 'desc', - unmapped_type: 'long', + order: 'desc' as const, + unmapped_type: 'long' as const, }, }, ], @@ -95,6 +95,7 @@ export async function fetchCCRReadExceptions( const { body: response } = await esClient.search(params); const stats: CCRReadExceptionsStats[] = []; + // @ts-expect-error @elastic/elasticsearch Aggregate does not specify buckets const { buckets: remoteClusterBuckets = [] } = response.aggregations.remote_clusters; if (!remoteClusterBuckets.length) { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_health.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_health.test.ts index d326c7f4bedda5..08ecaef33085bf 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_health.test.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_health.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { elasticsearchClientMock } from '../../../../../../src/core/server/elasticsearch/client/mocks'; import { fetchClusterHealth } from './fetch_cluster_health'; @@ -29,7 +30,7 @@ describe('fetchClusterHealth', () => { }, ], }, - }) + } as estypes.SearchResponse) ); const clusters = [{ clusterUuid, clusterName: 'foo' }]; diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_health.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_health.ts index be91aaa6ec983e..ddbf4e3d4b3c1b 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_health.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cluster_health.ts @@ -25,8 +25,8 @@ export async function fetchClusterHealth( sort: [ { timestamp: { - order: 'desc', - unmapped_type: 'long', + order: 'desc' as const, + unmapped_type: 'long' as const, }, }, ], @@ -59,11 +59,11 @@ export async function fetchClusterHealth( }, }; - const { body: response } = await esClient.search(params); - return response.hits.hits.map((hit: { _source: ElasticsearchSource; _index: string }) => { + const { body: response } = await esClient.search(params); + return response.hits.hits.map((hit) => { return { - health: hit._source.cluster_state?.status, - clusterUuid: hit._source.cluster_uuid, + health: hit._source!.cluster_state?.status, + clusterUuid: hit._source!.cluster_uuid, ccs: hit._index.includes(':') ? hit._index.split(':')[0] : undefined, } as AlertClusterHealth; }); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.test.ts index 54aa2e68d4ef25..75991e892d419f 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.test.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_clusters.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { elasticsearchClientMock } from 'src/core/server/elasticsearch/client/mocks'; import { elasticsearchServiceMock } from 'src/core/server/mocks'; @@ -28,7 +29,7 @@ describe('fetchClusters', () => { }, ], }, - }) + } as estypes.SearchResponse) ); const index = '.monitoring-es-*'; const result = await fetchClusters(esClient, index); @@ -57,7 +58,7 @@ describe('fetchClusters', () => { }, ], }, - }) + } as estypes.SearchResponse) ); const index = '.monitoring-es-*'; const result = await fetchClusters(esClient, index); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.test.ts index 2ff9ae3854e4ad..0fb9dd5298e9e1 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.test.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { elasticsearchClientMock } from '../../../../../../src/core/server/elasticsearch/client/mocks'; import { fetchCpuUsageNodeStats } from './fetch_cpu_usage_node_stats'; @@ -24,6 +25,7 @@ describe('fetchCpuUsageNodeStats', () => { it('fetch normal stats', async () => { esClient.search.mockReturnValue( + // @ts-expect-error @elastic/elasticsearch Aggregate only allows unknown values elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { clusters: { @@ -77,6 +79,7 @@ describe('fetchCpuUsageNodeStats', () => { it('fetch container stats', async () => { esClient.search.mockReturnValue( + // @ts-expect-error @elastic/elasticsearch Aggregate only allows unknown values elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { clusters: { @@ -143,6 +146,7 @@ describe('fetchCpuUsageNodeStats', () => { it('fetch properly return ccs', async () => { esClient.search.mockReturnValue( + // @ts-expect-error @elastic/elasticsearch Aggregate only allows unknown values elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { clusters: { @@ -193,7 +197,9 @@ describe('fetchCpuUsageNodeStats', () => { let params = null; esClient.search.mockImplementation((...args) => { params = args[0]; - return elasticsearchClientMock.createSuccessTransportRequestPromise({}); + return elasticsearchClientMock.createSuccessTransportRequestPromise( + {} as estypes.SearchResponse + ); }); await fetchCpuUsageNodeStats(esClient, clusters, index, startMs, endMs, size); expect(params).toStrictEqual({ diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.ts index 1dfbe381b99568..07ca3572ad6b3f 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_cpu_usage_node_stats.ts @@ -120,14 +120,14 @@ export async function fetchCpuUsageNodeStats( usage_deriv: { derivative: { buckets_path: 'average_usage', - gap_policy: 'skip', + gap_policy: 'skip' as const, unit: NORMALIZED_DERIVATIVE_UNIT, }, }, periods_deriv: { derivative: { buckets_path: 'average_periods', - gap_policy: 'skip', + gap_policy: 'skip' as const, unit: NORMALIZED_DERIVATIVE_UNIT, }, }, diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_disk_usage_node_stats.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_disk_usage_node_stats.test.ts index 7664d73f6009b0..8faf79fc4b59c6 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_disk_usage_node_stats.test.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_disk_usage_node_stats.test.ts @@ -25,6 +25,7 @@ describe('fetchDiskUsageNodeStats', () => { it('fetch normal stats', async () => { esClient.search.mockReturnValue( + // @ts-expect-error @elastic/elasticsearch Aggregate only allows unknown values elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { clusters: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_disk_usage_node_stats.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_disk_usage_node_stats.ts index aea4ede825d672..30daee225fcb4c 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_disk_usage_node_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_disk_usage_node_stats.ts @@ -101,7 +101,8 @@ export async function fetchDiskUsageNodeStats( const { body: response } = await esClient.search(params); const stats: AlertDiskUsageNodeStats[] = []; - const { buckets: clusterBuckets = [] } = response.aggregations.clusters; + // @ts-expect-error @elastic/elasticsearch Aggregate does not define buckets + const { buckets: clusterBuckets = [] } = response.aggregations!.clusters; if (!clusterBuckets.length) { return stats; diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_elasticsearch_versions.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_elasticsearch_versions.test.ts index be501ee3d52801..d1051748536364 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_elasticsearch_versions.test.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_elasticsearch_versions.test.ts @@ -9,6 +9,7 @@ import { elasticsearchClientMock } from '../../../../../../src/core/server/elasticsearch/client/mocks'; import { elasticsearchServiceMock } from 'src/core/server/mocks'; import { fetchElasticsearchVersions } from './fetch_elasticsearch_versions'; +import { estypes } from '@elastic/elasticsearch'; describe('fetchElasticsearchVersions', () => { const esClient = elasticsearchServiceMock.createScopedClusterClient().asCurrentUser; @@ -41,7 +42,7 @@ describe('fetchElasticsearchVersions', () => { }, ], }, - }) + } as estypes.SearchResponse) ); const result = await fetchElasticsearchVersions(esClient, clusters, index, size); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_elasticsearch_versions.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_elasticsearch_versions.ts index b4b7739f6731b7..111ef5b0c120d4 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_elasticsearch_versions.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_elasticsearch_versions.ts @@ -26,8 +26,8 @@ export async function fetchElasticsearchVersions( sort: [ { timestamp: { - order: 'desc', - unmapped_type: 'long', + order: 'desc' as const, + unmapped_type: 'long' as const, }, }, ], @@ -60,13 +60,13 @@ export async function fetchElasticsearchVersions( }, }; - const { body: response } = await esClient.search(params); - return response.hits.hits.map((hit: { _source: ElasticsearchSource; _index: string }) => { - const versions = hit._source.cluster_stats?.nodes?.versions; + const { body: response } = await esClient.search(params); + return response.hits.hits.map((hit) => { + const versions = hit._source!.cluster_stats?.nodes?.versions ?? []; return { versions, - clusterUuid: hit._source.cluster_uuid, - ccs: hit._index.includes(':') ? hit._index.split(':')[0] : null, + clusterUuid: hit._source!.cluster_uuid, + ccs: hit._index.includes(':') ? hit._index.split(':')[0] : undefined, }; }); } diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_index_shard_size.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_index_shard_size.ts index dfba0c42eef3dc..f51e1cde47f8d0 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_index_shard_size.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_index_shard_size.ts @@ -88,8 +88,8 @@ export async function fetchIndexShardSize( sort: [ { timestamp: { - order: 'desc', - unmapped_type: 'long', + order: 'desc' as const, + unmapped_type: 'long' as const, }, }, ], @@ -116,6 +116,7 @@ export async function fetchIndexShardSize( const { body: response } = await esClient.search(params); const stats: IndexShardSizeStats[] = []; + // @ts-expect-error @elastic/elasticsearch Aggregate does not specify buckets const { buckets: clusterBuckets = [] } = response.aggregations.clusters; const validIndexPatterns = memoizedIndexPatterns(shardIndexPatterns); diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_kibana_versions.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_kibana_versions.test.ts index 901851d7665127..2b966b16f2f5c6 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_kibana_versions.test.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_kibana_versions.test.ts @@ -23,6 +23,7 @@ describe('fetchKibanaVersions', () => { it('fetch as expected', async () => { esClient.search.mockReturnValue( + // @ts-expect-error @elastic/elasticsearch Aggregate only allows unknown values elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { index: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_kibana_versions.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_kibana_versions.ts index a4e1e606702ecd..cb2f201e2586ec 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_kibana_versions.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_kibana_versions.ts @@ -70,7 +70,7 @@ export async function fetchKibanaVersions( field: 'kibana_stats.kibana.version', size: 1, order: { - latest_report: 'desc', + latest_report: 'desc' as const, }, }, aggs: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.test.ts index 69a42812bfe885..3c12c70bf17133 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.test.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.test.ts @@ -8,6 +8,7 @@ import { fetchLicenses } from './fetch_licenses'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { elasticsearchClientMock } from '../../../../../../src/core/server/elasticsearch/client/mocks'; import { elasticsearchServiceMock } from 'src/core/server/mocks'; +import { estypes } from '@elastic/elasticsearch'; describe('fetchLicenses', () => { const clusterName = 'MyCluster'; @@ -32,7 +33,7 @@ describe('fetchLicenses', () => { }, ], }, - }) + } as estypes.SearchResponse) ); const clusters = [{ clusterUuid, clusterName }]; const index = '.monitoring-es-*'; diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.ts index 5cd4378f0a7470..5178b6c4c53a71 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_licenses.ts @@ -6,7 +6,7 @@ */ import { ElasticsearchClient } from 'kibana/server'; import { AlertLicense, AlertCluster } from '../../../common/types/alerts'; -import { ElasticsearchResponse } from '../../../common/types/es'; +import { ElasticsearchSource } from '../../../common/types/es'; export async function fetchLicenses( esClient: ElasticsearchClient, @@ -25,8 +25,8 @@ export async function fetchLicenses( sort: [ { timestamp: { - order: 'desc', - unmapped_type: 'long', + order: 'desc' as const, + unmapped_type: 'long' as const, }, }, ], @@ -59,15 +59,15 @@ export async function fetchLicenses( }, }; - const { body: response } = await esClient.search(params); + const { body: response } = await esClient.search(params); return ( response?.hits?.hits.map((hit) => { - const rawLicense = hit._source.license ?? {}; + const rawLicense = hit._source!.license ?? {}; const license: AlertLicense = { status: rawLicense.status ?? '', type: rawLicense.type ?? '', expiryDateMS: rawLicense.expiry_date_in_millis ?? 0, - clusterUuid: hit._source.cluster_uuid, + clusterUuid: hit._source!.cluster_uuid, ccs: hit._index, }; return license; diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_logstash_versions.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_logstash_versions.test.ts index e35de6e68866d1..d7d4e6531f58e2 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_logstash_versions.test.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_logstash_versions.test.ts @@ -23,6 +23,7 @@ describe('fetchLogstashVersions', () => { it('fetch as expected', async () => { esClient.search.mockReturnValue( + // @ts-expect-error @elastic/elasticsearch Aggregate only allows unknown values elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { index: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_logstash_versions.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_logstash_versions.ts index 6090ba36d97490..6fb54857d40e43 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_logstash_versions.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_logstash_versions.ts @@ -70,7 +70,7 @@ export async function fetchLogstashVersions( field: 'logstash_stats.logstash.version', size: 1, order: { - latest_report: 'desc', + latest_report: 'desc' as const, }, }, aggs: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_memory_usage_node_stats.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_memory_usage_node_stats.ts index 77c17a8ebf3eff..aad4638bf83599 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_memory_usage_node_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_memory_usage_node_stats.ts @@ -94,6 +94,7 @@ export async function fetchMemoryUsageNodeStats( const { body: response } = await esClient.search(params); const stats: AlertMemoryUsageNodeStats[] = []; + // @ts-expect-error @elastic/elasticsearch Aggregate does not define buckets const { buckets: clusterBuckets = [] } = response.aggregations.clusters; if (!clusterBuckets.length) { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_missing_monitoring_data.test.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_missing_monitoring_data.test.ts index 2388abf024eb95..c8d15acf8ff73a 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_missing_monitoring_data.test.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_missing_monitoring_data.test.ts @@ -56,6 +56,7 @@ describe('fetchMissingMonitoringData', () => { ]; esClient.search.mockReturnValue( + // @ts-expect-error @elastic/elasticsearch Aggregate only allows unknown values elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { clusters: { @@ -114,6 +115,7 @@ describe('fetchMissingMonitoringData', () => { }, ]; esClient.search.mockReturnValue( + // @ts-expect-error @elastic/elasticsearch Aggregate only allows unknown values elasticsearchClientMock.createSuccessTransportRequestPromise({ aggregations: { clusters: { diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_missing_monitoring_data.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_missing_monitoring_data.ts index cb274848e6c5af..a7b4a3a0232072 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_missing_monitoring_data.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_missing_monitoring_data.ts @@ -99,8 +99,8 @@ export async function fetchMissingMonitoringData( sort: [ { timestamp: { - order: 'desc', - unmapped_type: 'long', + order: 'desc' as const, + unmapped_type: 'long' as const, }, }, ], diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_nodes_from_cluster_stats.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_nodes_from_cluster_stats.ts index a97594c8ca9956..ff3a8d4aa7ef8f 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_nodes_from_cluster_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_nodes_from_cluster_stats.ts @@ -36,8 +36,8 @@ export async function fetchNodesFromClusterStats( sort: [ { timestamp: { - order: 'desc', - unmapped_type: 'long', + order: 'desc' as const, + unmapped_type: 'long' as const, }, }, ], @@ -71,8 +71,8 @@ export async function fetchNodesFromClusterStats( sort: [ { timestamp: { - order: 'desc', - unmapped_type: 'long', + order: 'desc' as const, + unmapped_type: 'long' as const, }, }, ], @@ -90,6 +90,7 @@ export async function fetchNodesFromClusterStats( const { body: response } = await esClient.search(params); const nodes = []; + // @ts-expect-error @elastic/elasticsearch Aggregate does not define buckets const clusterBuckets = response.aggregations.clusters.buckets; for (const clusterBucket of clusterBuckets) { const clusterUuid = clusterBucket.key; diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_thread_pool_rejections_stats.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_thread_pool_rejections_stats.ts index 5770721195e141..b63244dab719d8 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_thread_pool_rejections_stats.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_thread_pool_rejections_stats.ts @@ -13,13 +13,13 @@ const invalidNumberValue = (value: number) => { return isNaN(value) || value === undefined || value === null; }; -const getTopHits = (threadType: string, order: string) => ({ +const getTopHits = (threadType: string, order: 'asc' | 'desc') => ({ top_hits: { sort: [ { timestamp: { order, - unmapped_type: 'long', + unmapped_type: 'long' as const, }, }, ], @@ -81,10 +81,10 @@ export async function fetchThreadPoolRejectionStats( }, aggs: { most_recent: { - ...getTopHits(threadType, 'desc'), + ...getTopHits(threadType, 'desc' as const), }, least_recent: { - ...getTopHits(threadType, 'asc'), + ...getTopHits(threadType, 'asc' as const), }, }, }, @@ -96,6 +96,7 @@ export async function fetchThreadPoolRejectionStats( const { body: response } = await esClient.search(params); const stats: AlertThreadPoolRejectionsStats[] = []; + // @ts-expect-error @elastic/elasticsearch Aggregate does not specify buckets const { buckets: clusterBuckets = [] } = response.aggregations.clusters; if (!clusterBuckets.length) { diff --git a/x-pack/plugins/observability/server/index.ts b/x-pack/plugins/observability/server/index.ts index e118d17e17c3fd..2676e40a4902f4 100644 --- a/x-pack/plugins/observability/server/index.ts +++ b/x-pack/plugins/observability/server/index.ts @@ -8,7 +8,7 @@ import { schema, TypeOf } from '@kbn/config-schema'; import { PluginInitializerContext } from 'src/core/server'; import { ObservabilityPlugin, ObservabilityPluginSetup } from './plugin'; -import { createOrUpdateIndex, MappingsDefinition } from './utils/create_or_update_index'; +import { createOrUpdateIndex, Mappings } from './utils/create_or_update_index'; import { ScopedAnnotationsClient } from './lib/annotations/bootstrap_annotations'; import { unwrapEsResponse, WrappedElasticsearchClientError } from './utils/unwrap_es_response'; @@ -29,7 +29,7 @@ export const plugin = (initContext: PluginInitializerContext) => export { createOrUpdateIndex, - MappingsDefinition, + Mappings, ObservabilityPluginSetup, ScopedAnnotationsClient, unwrapEsResponse, diff --git a/x-pack/plugins/observability/server/lib/annotations/create_annotations_client.ts b/x-pack/plugins/observability/server/lib/annotations/create_annotations_client.ts index 7abd68cb9f16c9..39a594dcc86ca9 100644 --- a/x-pack/plugins/observability/server/lib/annotations/create_annotations_client.ts +++ b/x-pack/plugins/observability/server/lib/annotations/create_annotations_client.ts @@ -23,27 +23,6 @@ type CreateParams = t.TypeOf; type DeleteParams = t.TypeOf; type GetByIdParams = t.TypeOf; -interface IndexDocumentResponse { - _shards: { - total: number; - failed: number; - successful: number; - }; - _index: string; - _type: string; - _id: string; - _version: number; - _seq_no: number; - _primary_term: number; - result: string; -} - -export interface GetResponse { - _id: string; - _index: string; - _source: Annotation; -} - export function createAnnotationsClient(params: { index: string; esClient: ElasticsearchClient; @@ -95,7 +74,7 @@ export function createAnnotationsClient(params: { }; const body = await unwrapEsResponse( - esClient.index({ + esClient.index({ index, body: annotation, refresh: 'wait_for', @@ -103,18 +82,18 @@ export function createAnnotationsClient(params: { ); return ( - await esClient.get({ + await esClient.get({ index, id: body._id, }) - ).body; + ).body as { _id: string; _index: string; _source: Annotation }; } ), getById: ensureGoldLicense(async (getByIdParams: GetByIdParams) => { const { id } = getByIdParams; return unwrapEsResponse( - esClient.get({ + esClient.get({ id, index, }) diff --git a/x-pack/plugins/observability/server/lib/annotations/mappings.ts b/x-pack/plugins/observability/server/lib/annotations/mappings.ts index 3313c411b58890..da72afdbecb33a 100644 --- a/x-pack/plugins/observability/server/lib/annotations/mappings.ts +++ b/x-pack/plugins/observability/server/lib/annotations/mappings.ts @@ -6,7 +6,7 @@ */ export const mappings = { - dynamic: 'strict', + dynamic: 'strict' as const, properties: { annotation: { properties: { @@ -45,4 +45,4 @@ export const mappings = { }, }, }, -} as const; +}; diff --git a/x-pack/plugins/observability/server/utils/create_or_update_index.ts b/x-pack/plugins/observability/server/utils/create_or_update_index.ts index cc6504fd4d4fd9..19b14ef8b24375 100644 --- a/x-pack/plugins/observability/server/utils/create_or_update_index.ts +++ b/x-pack/plugins/observability/server/utils/create_or_update_index.ts @@ -4,24 +4,12 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import { CreateIndexRequest, PutMappingRequest } from '@elastic/elasticsearch/api/types'; import pRetry from 'p-retry'; import { Logger, ElasticsearchClient } from 'src/core/server'; -export interface MappingsObject { - type: string; - ignore_above?: number; - scaling_factor?: number; - ignore_malformed?: boolean; - coerce?: boolean; - fields?: Record; -} - -export interface MappingsDefinition { - dynamic?: boolean | 'strict'; - properties: Record; - dynamic_templates?: any[]; -} +export type Mappings = Required['body']['mappings'] & + Required['body']; export async function createOrUpdateIndex({ index, @@ -30,7 +18,7 @@ export async function createOrUpdateIndex({ logger, }: { index: string; - mappings: MappingsDefinition; + mappings: Mappings; client: ElasticsearchClient; logger: Logger; }) { @@ -59,7 +47,8 @@ export async function createOrUpdateIndex({ }); if (!result.body.acknowledged) { - const resultError = result && result.body.error && JSON.stringify(result.body.error); + const bodyWithError: { body?: { error: any } } = result as any; + const resultError = JSON.stringify(bodyWithError?.body?.error); throw new Error(resultError); } }, @@ -82,9 +71,9 @@ function createNewIndex({ }: { index: string; client: ElasticsearchClient; - mappings: MappingsDefinition; + mappings: Required['body']['mappings']; }) { - return client.indices.create<{ acknowledged: boolean; error: any }>({ + return client.indices.create({ index, body: { // auto_expand_replicas: Allows cluster to not have replicas for this index @@ -101,9 +90,9 @@ function updateExistingIndex({ }: { index: string; client: ElasticsearchClient; - mappings: MappingsDefinition; + mappings: PutMappingRequest['body']; }) { - return client.indices.putMapping<{ acknowledged: boolean; error: any }>({ + return client.indices.putMapping({ index, body: mappings, }); diff --git a/x-pack/plugins/osquery/common/search_strategy/osquery/actions/index.ts b/x-pack/plugins/osquery/common/search_strategy/osquery/actions/index.ts index 561866d5077a6b..b24e4f28d89f11 100644 --- a/x-pack/plugins/osquery/common/search_strategy/osquery/actions/index.ts +++ b/x-pack/plugins/osquery/common/search_strategy/osquery/actions/index.ts @@ -5,15 +5,15 @@ * 2.0. */ -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { IEsSearchResponse } from '../../../../../../../src/plugins/data/common'; import { Inspect, Maybe, PageInfoPaginated } from '../../common'; import { RequestOptions, RequestOptionsPaginated } from '../..'; -export type ActionEdges = SearchResponse['hits']['hits']; +export type ActionEdges = estypes.SearchResponse['hits']['hits']; -export type ActionResultEdges = SearchResponse['hits']['hits']; +export type ActionResultEdges = estypes.SearchResponse['hits']['hits']; export interface ActionsStrategyResponse extends IEsSearchResponse { edges: ActionEdges; totalCount: number; diff --git a/x-pack/plugins/osquery/common/search_strategy/osquery/results/index.ts b/x-pack/plugins/osquery/common/search_strategy/osquery/results/index.ts index add8598bb77ad6..035774aaffe36d 100644 --- a/x-pack/plugins/osquery/common/search_strategy/osquery/results/index.ts +++ b/x-pack/plugins/osquery/common/search_strategy/osquery/results/index.ts @@ -5,13 +5,13 @@ * 2.0. */ -import { SearchResponse } from 'elasticsearch'; +import { estypes } from '@elastic/elasticsearch'; import { IEsSearchResponse } from '../../../../../../../src/plugins/data/common'; import { Inspect, Maybe, PageInfoPaginated } from '../../common'; import { RequestOptionsPaginated } from '../..'; -export type ResultEdges = SearchResponse['hits']['hits']; +export type ResultEdges = estypes.SearchResponse['hits']['hits']; export interface ResultsStrategyResponse extends IEsSearchResponse { edges: ResultEdges; diff --git a/x-pack/plugins/osquery/public/action_results/action_results_table.tsx b/x-pack/plugins/osquery/public/action_results/action_results_table.tsx index 1dd5b63eedc23a..1880cec0ae8e20 100644 --- a/x-pack/plugins/osquery/public/action_results/action_results_table.tsx +++ b/x-pack/plugins/osquery/public/action_results/action_results_table.tsx @@ -107,7 +107,7 @@ const ActionResultsTableComponent: React.FC = ({ action if (columnId === 'status') { // eslint-disable-next-line react-hooks/rules-of-hooks const linkProps = useRouterNavigate( - `/live_query/${actionId}/results/${value.fields.agent_id[0]}` + `/live_query/${actionId}/results/${value.fields?.agent_id[0]}` ); return ( @@ -122,7 +122,7 @@ const ActionResultsTableComponent: React.FC = ({ action // eslint-disable-next-line react-hooks/rules-of-hooks const { data: allResultsData } = useAllResults({ actionId, - agentId: value.fields.agent_id[0], + agentId: value.fields?.agent_id[0], activePage: pagination.pageIndex, limit: pagination.pageSize, direction: Direction.asc, @@ -133,7 +133,7 @@ const ActionResultsTableComponent: React.FC = ({ action } if (columnId === 'agent_status') { - const agentIdValue = value.fields.agent_id[0]; + const agentIdValue = value.fields?.agent_id[0]; // @ts-expect-error update types const agent = find(['_id', agentIdValue], agentsData?.agents); const online = agent?.active; @@ -143,7 +143,7 @@ const ActionResultsTableComponent: React.FC = ({ action } if (columnId === 'agent') { - const agentIdValue = value.fields.agent_id[0]; + const agentIdValue = value.fields?.agent_id[0]; // @ts-expect-error update types const agent = find(['_id', agentIdValue], agentsData?.agents); const agentName = agent?.local_metadata.host.name; @@ -156,6 +156,7 @@ const ActionResultsTableComponent: React.FC = ({ action } if (columnId === '@timestamp') { + // @ts-expect-error fields is optional return value.fields['@timestamp']; } diff --git a/x-pack/plugins/osquery/public/actions/actions_table.tsx b/x-pack/plugins/osquery/public/actions/actions_table.tsx index 986b46b1a40899..ca856938496514 100644 --- a/x-pack/plugins/osquery/public/actions/actions_table.tsx +++ b/x-pack/plugins/osquery/public/actions/actions_table.tsx @@ -62,6 +62,7 @@ const ActionsTableComponent = () => { () => ({ rowIndex, columnId }) => { // eslint-disable-next-line react-hooks/rules-of-hooks const data = useContext(DataContext); + // @ts-expect-error fields is optional const value = data[rowIndex].fields[columnId]; if (columnId === 'action_id') { diff --git a/x-pack/plugins/osquery/public/results/results_table.tsx b/x-pack/plugins/osquery/public/results/results_table.tsx index 4c2048148f745f..7557828c4407cc 100644 --- a/x-pack/plugins/osquery/public/results/results_table.tsx +++ b/x-pack/plugins/osquery/public/results/results_table.tsx @@ -68,9 +68,11 @@ const ResultsTableComponent: React.FC = ({ actionId, // eslint-disable-next-line react-hooks/rules-of-hooks const data = useContext(DataContext); + // @ts-expect-error fields is optional const value = data[rowIndex].fields[columnId]; if (columnId === 'agent.name') { + // @ts-expect-error fields is optional const agentIdValue = data[rowIndex].fields['agent.id']; // eslint-disable-next-line react-hooks/rules-of-hooks const linkProps = useRouterNavigate(`/live_query/${actionId}/results/${agentIdValue}`); diff --git a/x-pack/plugins/osquery/server/search_strategy/osquery/factory/actions/all/index.ts b/x-pack/plugins/osquery/server/search_strategy/osquery/factory/actions/all/index.ts index bc0af9a1d0dcc7..e05bd15bcc7225 100644 --- a/x-pack/plugins/osquery/server/search_strategy/osquery/factory/actions/all/index.ts +++ b/x-pack/plugins/osquery/server/search_strategy/osquery/factory/actions/all/index.ts @@ -36,6 +36,7 @@ export const allActions: OsqueryFactory = { ...response, inspect, edges: response.rawResponse.hits.hits, + // @ts-expect-error doesn't handle case when total TotalHits totalCount: response.rawResponse.hits.total, pageInfo: { activePage: activePage ?? 0, diff --git a/x-pack/plugins/osquery/server/search_strategy/osquery/factory/actions/results/index.ts b/x-pack/plugins/osquery/server/search_strategy/osquery/factory/actions/results/index.ts index dbcc03006399af..50fcf938bdd793 100644 --- a/x-pack/plugins/osquery/server/search_strategy/osquery/factory/actions/results/index.ts +++ b/x-pack/plugins/osquery/server/search_strategy/osquery/factory/actions/results/index.ts @@ -37,6 +37,7 @@ export const actionResults: OsqueryFactory = { ...response, inspect, edges: response.rawResponse.hits.hits, + // @ts-expect-error doesn't handle case when total TotalHits totalCount: response.rawResponse.hits.total, pageInfo: { activePage: activePage ?? 0, diff --git a/x-pack/plugins/osquery/server/search_strategy/osquery/factory/agents/index.ts b/x-pack/plugins/osquery/server/search_strategy/osquery/factory/agents/index.ts index 1f7fbccb686827..7e0532dfec1af0 100644 --- a/x-pack/plugins/osquery/server/search_strategy/osquery/factory/agents/index.ts +++ b/x-pack/plugins/osquery/server/search_strategy/osquery/factory/agents/index.ts @@ -37,7 +37,11 @@ export const allAgents: OsqueryFactory = { return { ...response, inspect, - edges: response.rawResponse.hits.hits.map((hit) => ({ _id: hit._id, ...hit._source })), + edges: response.rawResponse.hits.hits.map((hit) => ({ + _id: hit._id, + ...hit._source, + })) as Agent[], + // @ts-expect-error doesn't handle case when total TotalHits totalCount: response.rawResponse.hits.total, pageInfo: { activePage: activePage ?? 0, diff --git a/x-pack/plugins/osquery/server/search_strategy/osquery/factory/results/index.ts b/x-pack/plugins/osquery/server/search_strategy/osquery/factory/results/index.ts index 7b9a24e4a06533..93cba882e39ed5 100644 --- a/x-pack/plugins/osquery/server/search_strategy/osquery/factory/results/index.ts +++ b/x-pack/plugins/osquery/server/search_strategy/osquery/factory/results/index.ts @@ -36,6 +36,7 @@ export const allResults: OsqueryFactory = { ...response, inspect, edges: response.rawResponse.hits.hits, + // @ts-expect-error doesn't handle case when total TotalHits totalCount: response.rawResponse.hits.total, pageInfo: { activePage: activePage ?? 0, diff --git a/x-pack/plugins/painless_lab/server/routes/api/execute.ts b/x-pack/plugins/painless_lab/server/routes/api/execute.ts index e77f4fd4a05b5a..03168098052008 100644 --- a/x-pack/plugins/painless_lab/server/routes/api/execute.ts +++ b/x-pack/plugins/painless_lab/server/routes/api/execute.ts @@ -27,6 +27,7 @@ export function registerExecuteRoute({ router, license }: RouteDependencies) { try { const client = ctx.core.elasticsearch.client.asCurrentUser; const response = await client.scriptsPainlessExecute({ + // @ts-expect-error `ExecutePainlessScriptRequest.body` does not allow `string` body, }); diff --git a/x-pack/plugins/reporting/server/export_types/csv_searchsource/generate_csv/generate_csv.ts b/x-pack/plugins/reporting/server/export_types/csv_searchsource/generate_csv/generate_csv.ts index 370fc42921acf4..85c5379a63b7fd 100644 --- a/x-pack/plugins/reporting/server/export_types/csv_searchsource/generate_csv/generate_csv.ts +++ b/x-pack/plugins/reporting/server/export_types/csv_searchsource/generate_csv/generate_csv.ts @@ -91,7 +91,7 @@ export class CsvGenerator { }; const results = ( await this.clients.data.search(searchParams, { strategy: ES_SEARCH_STRATEGY }).toPromise() - ).rawResponse; + ).rawResponse as SearchResponse; return results; } diff --git a/x-pack/plugins/reporting/server/usage/get_reporting_usage.ts b/x-pack/plugins/reporting/server/usage/get_reporting_usage.ts index 1eb11033368019..f07da188f3e62e 100644 --- a/x-pack/plugins/reporting/server/usage/get_reporting_usage.ts +++ b/x-pack/plugins/reporting/server/usage/get_reporting_usage.ts @@ -4,9 +4,9 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import { get } from 'lodash'; -import { ElasticsearchClient, SearchResponse } from 'kibana/server'; +import { ElasticsearchClient } from 'kibana/server'; import { ReportingConfig } from '../'; import { ExportTypesRegistry } from '../lib/export_types_registry'; import { GetLicense } from './'; @@ -19,7 +19,7 @@ import { KeyCountBucket, RangeStats, ReportingUsageType, - ReportingUsageSearchResponse, + // ReportingUsageSearchResponse, StatusByAppBucket, } from './types'; @@ -100,7 +100,8 @@ type RangeStatSets = Partial & { last7Days: Partial; }; -type ESResponse = Partial>; +// & ReportingUsageSearchResponse +type ESResponse = Partial; async function handleResponse(response: ESResponse): Promise> { const buckets = get(response, 'aggregations.ranges.buckets'); diff --git a/x-pack/plugins/saved_objects_tagging/server/usage/fetch_tag_usage_data.ts b/x-pack/plugins/saved_objects_tagging/server/usage/fetch_tag_usage_data.ts index 3e745458f66074..06230172d52bd3 100644 --- a/x-pack/plugins/saved_objects_tagging/server/usage/fetch_tag_usage_data.ts +++ b/x-pack/plugins/saved_objects_tagging/server/usage/fetch_tag_usage_data.ts @@ -11,26 +11,22 @@ import { TaggingUsageData, ByTypeTaggingUsageData } from './types'; /** * Manual type reflection of the `tagDataAggregations` resulting payload */ -interface AggregatedTagUsageResponseBody { - aggregations: { - by_type: { - buckets: Array<{ - key: string; +interface AggregatedTagUsage { + buckets: Array<{ + key: string; + doc_count: number; + nested_ref: { + tag_references: { doc_count: number; - nested_ref: { - tag_references: { + tag_id: { + buckets: Array<{ + key: string; doc_count: number; - tag_id: { - buckets: Array<{ - key: string; - doc_count: number; - }>; - }; - }; + }>; }; - }>; + }; }; - }; + }>; } export const fetchTagUsageData = async ({ @@ -40,7 +36,7 @@ export const fetchTagUsageData = async ({ esClient: ElasticsearchClient; kibanaIndex: string; }): Promise => { - const { body } = await esClient.search({ + const { body } = await esClient.search({ index: [kibanaIndex], ignore_unavailable: true, filter_path: 'aggregations', @@ -59,7 +55,7 @@ export const fetchTagUsageData = async ({ const allUsedTags = new Set(); let totalTaggedObjects = 0; - const typeBuckets = body.aggregations.by_type.buckets; + const typeBuckets = (body.aggregations!.by_type as AggregatedTagUsage).buckets; typeBuckets.forEach((bucket) => { const type = bucket.key; const taggedDocCount = bucket.doc_count; diff --git a/x-pack/plugins/searchprofiler/server/routes/profile.ts b/x-pack/plugins/searchprofiler/server/routes/profile.ts index cdc420667f9e10..cbe0b75bc9eda0 100644 --- a/x-pack/plugins/searchprofiler/server/routes/profile.ts +++ b/x-pack/plugins/searchprofiler/server/routes/profile.ts @@ -33,16 +33,15 @@ export const register = ({ router, getLicenseStatus, log }: RouteDependencies) = body: { query, index }, } = request; - const parsed = { - // Activate profiler mode for this query. - profile: true, - ...query, - }; - const body = { index, - body: JSON.stringify(parsed, null, 2), + body: { + // Activate profiler mode for this query. + profile: true, + ...query, + }, }; + try { const client = ctx.core.elasticsearch.client.asCurrentUser; const resp = await client.search(body); diff --git a/x-pack/plugins/security/common/model/authenticated_user.mock.ts b/x-pack/plugins/security/common/model/authenticated_user.mock.ts index 3715245b37e61a..6dad3886401ac1 100644 --- a/x-pack/plugins/security/common/model/authenticated_user.mock.ts +++ b/x-pack/plugins/security/common/model/authenticated_user.mock.ts @@ -7,7 +7,10 @@ import type { AuthenticatedUser } from './authenticated_user'; -export function mockAuthenticatedUser(user: Partial = {}) { +// We omit `roles` here since the original interface defines this field as `readonly string[]` that makes it hard to use +// in various mocks that expect mutable string array. +type AuthenticatedUserProps = Partial & { roles: string[] }>; +export function mockAuthenticatedUser(user: AuthenticatedUserProps = {}) { return { username: 'user', email: 'email', @@ -18,6 +21,7 @@ export function mockAuthenticatedUser(user: Partial = {}) { lookup_realm: { name: 'native1', type: 'native' }, authentication_provider: { type: 'basic', name: 'basic1' }, authentication_type: 'realm', + metadata: { _reserved: false }, ...user, }; } diff --git a/x-pack/plugins/security/server/authentication/api_keys/api_keys.test.ts b/x-pack/plugins/security/server/authentication/api_keys/api_keys.test.ts index f0a832f451cb81..bb27a53d341a3b 100644 --- a/x-pack/plugins/security/server/authentication/api_keys/api_keys.test.ts +++ b/x-pack/plugins/security/server/authentication/api_keys/api_keys.test.ts @@ -64,7 +64,14 @@ describe('API Keys', () => { it('returns true when the operation completes without error', async () => { mockLicense.isEnabled.mockReturnValue(true); mockClusterClient.asInternalUser.security.invalidateApiKey.mockResolvedValue( - securityMock.createApiResponse({ body: {} }) + securityMock.createApiResponse({ + body: { + invalidated_api_keys: [], + previously_invalidated_api_keys: [], + error_count: 0, + error_details: [], + }, + }) ); const result = await apiKeys.areAPIKeysEnabled(); expect(mockClusterClient.asInternalUser.security.invalidateApiKey).toHaveBeenCalledTimes(1); @@ -105,7 +112,14 @@ describe('API Keys', () => { it('calls `invalidateApiKey` with proper parameters', async () => { mockLicense.isEnabled.mockReturnValue(true); mockClusterClient.asInternalUser.security.invalidateApiKey.mockResolvedValueOnce( - securityMock.createApiResponse({ body: {} }) + securityMock.createApiResponse({ + body: { + invalidated_api_keys: [], + previously_invalidated_api_keys: [], + error_count: 0, + error_details: [], + }, + }) ); const result = await apiKeys.areAPIKeysEnabled(); @@ -133,6 +147,7 @@ describe('API Keys', () => { mockLicense.isEnabled.mockReturnValue(true); mockScopedClusterClient.asCurrentUser.security.createApiKey.mockResolvedValueOnce( + // @ts-expect-error @elastic/elsticsearch CreateApiKeyResponse.expiration: number securityMock.createApiResponse({ body: { id: '123', @@ -302,6 +317,7 @@ describe('API Keys', () => { invalidated_api_keys: ['api-key-id-1'], previously_invalidated_api_keys: [], error_count: 0, + error_details: [], }, }) ); @@ -312,6 +328,7 @@ describe('API Keys', () => { invalidated_api_keys: ['api-key-id-1'], previously_invalidated_api_keys: [], error_count: 0, + error_details: [], }); expect(mockScopedClusterClient.asCurrentUser.security.invalidateApiKey).toHaveBeenCalledWith({ body: { @@ -328,6 +345,7 @@ describe('API Keys', () => { invalidated_api_keys: ['api-key-id-1'], previously_invalidated_api_keys: [], error_count: 0, + error_details: [], }, }) ); @@ -339,6 +357,7 @@ describe('API Keys', () => { invalidated_api_keys: ['api-key-id-1'], previously_invalidated_api_keys: [], error_count: 0, + error_details: [], }); expect(mockScopedClusterClient.asCurrentUser.security.invalidateApiKey).toHaveBeenCalledWith({ body: { @@ -364,6 +383,7 @@ describe('API Keys', () => { invalidated_api_keys: ['api-key-id-1'], previously_invalidated_api_keys: [], error_count: 0, + error_details: [], }, }) ); @@ -372,6 +392,7 @@ describe('API Keys', () => { invalidated_api_keys: ['api-key-id-1'], previously_invalidated_api_keys: [], error_count: 0, + error_details: [], }); expect(mockClusterClient.asInternalUser.security.invalidateApiKey).toHaveBeenCalledWith({ body: { @@ -388,6 +409,7 @@ describe('API Keys', () => { invalidated_api_keys: ['api-key-id-1'], previously_invalidated_api_keys: [], error_count: 0, + error_details: [], }, }) ); @@ -399,6 +421,7 @@ describe('API Keys', () => { invalidated_api_keys: ['api-key-id-1'], previously_invalidated_api_keys: [], error_count: 0, + error_details: [], }); expect(mockClusterClient.asInternalUser.security.invalidateApiKey).toHaveBeenCalledWith({ body: { diff --git a/x-pack/plugins/security/server/authentication/api_keys/api_keys.ts b/x-pack/plugins/security/server/authentication/api_keys/api_keys.ts index 1b4b9d04603fb3..1af9058bd65a1f 100644 --- a/x-pack/plugins/security/server/authentication/api_keys/api_keys.ts +++ b/x-pack/plugins/security/server/authentication/api_keys/api_keys.ts @@ -109,7 +109,7 @@ export interface InvalidateAPIKeyResult { error_details?: Array<{ type: string; reason: string; - caused_by: { + caused_by?: { type: string; reason: string; }; @@ -145,7 +145,7 @@ export class APIKeys { ); try { - await this.clusterClient.asInternalUser.security.invalidateApiKey({ body: { id } }); + await this.clusterClient.asInternalUser.security.invalidateApiKey({ body: { ids: [id], } }); return true; } catch (e) { if (this.doesErrorIndicateAPIKeysAreDisabled(e)) { @@ -171,12 +171,12 @@ export class APIKeys { this.logger.debug('Trying to create an API key'); // User needs `manage_api_key` privilege to use this API - let result; + let result: CreateAPIKeyResult; try { result = ( await this.clusterClient .asScoped(request) - .asCurrentUser.security.createApiKey({ body: params }) + .asCurrentUser.security.createApiKey({ body: params }) ).body; this.logger.debug('API key was created successfully'); } catch (e) { @@ -207,10 +207,11 @@ export class APIKeys { const params = this.getGrantParams(createParams, authorizationHeader); // User needs `manage_api_key` or `grant_api_key` privilege to use this API - let result; + let result: GrantAPIKeyResult; try { result = ( - await this.clusterClient.asInternalUser.security.grantApiKey({ + await this.clusterClient.asInternalUser.security.grantApiKey({ + // @ts-expect-error @elastic/elasticsearch api_key.role_descriptors body: params, }) ).body; @@ -235,15 +236,15 @@ export class APIKeys { this.logger.debug(`Trying to invalidate ${params.ids.length} an API key as current user`); - let result; + let result: InvalidateAPIKeyResult; try { // User needs `manage_api_key` privilege to use this API result = ( - await this.clusterClient - .asScoped(request) - .asCurrentUser.security.invalidateApiKey({ - body: { ids: params.ids }, - }) + await this.clusterClient.asScoped(request).asCurrentUser.security.invalidateApiKey({ + body: { + ids: params.ids, + }, + }) ).body; this.logger.debug( `API keys by ids=[${params.ids.join(', ')}] was invalidated successfully as current user` @@ -271,12 +272,14 @@ export class APIKeys { this.logger.debug(`Trying to invalidate ${params.ids.length} API keys`); - let result; + let result: InvalidateAPIKeyResult; try { // Internal user needs `cluster:admin/xpack/security/api_key/invalidate` privilege to use this API result = ( - await this.clusterClient.asInternalUser.security.invalidateApiKey({ - body: { ids: params.ids }, + await this.clusterClient.asInternalUser.security.invalidateApiKey({ + body: { + ids: params.ids, + }, }) ).body; this.logger.debug(`API keys by ids=[${params.ids.join(', ')}] was invalidated successfully`); diff --git a/x-pack/plugins/security/server/authentication/providers/base.ts b/x-pack/plugins/security/server/authentication/providers/base.ts index 4750b9c325e9cb..ddbbbcc9f8cd6f 100644 --- a/x-pack/plugins/security/server/authentication/providers/base.ts +++ b/x-pack/plugins/security/server/authentication/providers/base.ts @@ -114,10 +114,11 @@ export abstract class BaseAuthenticationProvider { */ protected async getUser(request: KibanaRequest, authHeaders: Headers = {}) { return this.authenticationInfoToAuthenticatedUser( + // @ts-expect-error @elastic/elasticsearch `AuthenticateResponse` type doesn't define `authentication_type` and `enabled`. ( await this.options.client .asScoped({ headers: { ...request.headers, ...authHeaders } }) - .asCurrentUser.security.authenticate() + .asCurrentUser.security.authenticate() ).body ); } diff --git a/x-pack/plugins/security/server/authentication/providers/kerberos.test.ts b/x-pack/plugins/security/server/authentication/providers/kerberos.test.ts index 1bcb845ca5c084..f5c02953cebd36 100644 --- a/x-pack/plugins/security/server/authentication/providers/kerberos.test.ts +++ b/x-pack/plugins/security/server/authentication/providers/kerberos.test.ts @@ -46,7 +46,7 @@ describe('KerberosAuthenticationProvider', () => { const mockScopedClusterClient = elasticsearchServiceMock.createScopedClusterClient(); mockScopedClusterClient.asCurrentUser.security.authenticate.mockResolvedValue( - securityMock.createApiResponse({ body: {} }) + securityMock.createApiResponse({ body: mockAuthenticatedUser() }) ); mockOptions.client.asScoped.mockReturnValue(mockScopedClusterClient); @@ -122,6 +122,7 @@ describe('KerberosAuthenticationProvider', () => { }); mockOptions.client.asInternalUser.security.getToken.mockResolvedValue( + // @ts-expect-error not full interface securityMock.createApiResponse({ body: { access_token: 'some-token', @@ -156,6 +157,7 @@ describe('KerberosAuthenticationProvider', () => { }); mockOptions.client.asInternalUser.security.getToken.mockResolvedValue( + // @ts-expect-error not full interface securityMock.createApiResponse({ body: { access_token: 'some-token', diff --git a/x-pack/plugins/security/server/authentication/providers/kerberos.ts b/x-pack/plugins/security/server/authentication/providers/kerberos.ts index 0de8e8e10a630c..75dc2a8f479694 100644 --- a/x-pack/plugins/security/server/authentication/providers/kerberos.ts +++ b/x-pack/plugins/security/server/authentication/providers/kerberos.ts @@ -155,9 +155,13 @@ export class KerberosAuthenticationProvider extends BaseAuthenticationProvider { authentication: AuthenticationInfo; }; try { + // @ts-expect-error authentication.email can be optional tokens = ( - await this.options.client.asInternalUser.security.getToken({ - body: { grant_type: '_kerberos', kerberos_ticket: kerberosTicket }, + await this.options.client.asInternalUser.security.getToken({ + body: { + grant_type: '_kerberos', + kerberos_ticket: kerberosTicket, + }, }) ).body; } catch (err) { diff --git a/x-pack/plugins/security/server/authentication/providers/oidc.test.ts b/x-pack/plugins/security/server/authentication/providers/oidc.test.ts index f35545e5e5f3ac..ebeca42682eb94 100644 --- a/x-pack/plugins/security/server/authentication/providers/oidc.test.ts +++ b/x-pack/plugins/security/server/authentication/providers/oidc.test.ts @@ -11,7 +11,6 @@ import Boom from '@hapi/boom'; import type { KibanaRequest } from 'src/core/server'; import { elasticsearchServiceMock, httpServerMock } from 'src/core/server/mocks'; -import type { AuthenticatedUser } from '../../../common/model'; import { mockAuthenticatedUser } from '../../../common/model/authenticated_user.mock'; import { securityMock } from '../../mocks'; import { AuthenticationResult } from '../authentication_result'; @@ -24,7 +23,7 @@ import { OIDCAuthenticationProvider, OIDCLogin } from './oidc'; describe('OIDCAuthenticationProvider', () => { let provider: OIDCAuthenticationProvider; let mockOptions: MockAuthenticationProviderOptions; - let mockUser: AuthenticatedUser; + let mockUser: ReturnType; let mockScopedClusterClient: ReturnType< typeof elasticsearchServiceMock.createScopedClusterClient >; diff --git a/x-pack/plugins/security/server/authentication/providers/saml.test.ts b/x-pack/plugins/security/server/authentication/providers/saml.test.ts index f52b9129cb335f..4dcab1ed09b5a0 100644 --- a/x-pack/plugins/security/server/authentication/providers/saml.test.ts +++ b/x-pack/plugins/security/server/authentication/providers/saml.test.ts @@ -10,7 +10,6 @@ import Boom from '@hapi/boom'; import { elasticsearchServiceMock, httpServerMock } from 'src/core/server/mocks'; -import type { AuthenticatedUser } from '../../../common/model'; import { mockAuthenticatedUser } from '../../../common/model/authenticated_user.mock'; import { securityMock } from '../../mocks'; import { AuthenticationResult } from '../authentication_result'; @@ -22,7 +21,7 @@ import { SAMLAuthenticationProvider, SAMLLogin } from './saml'; describe('SAMLAuthenticationProvider', () => { let provider: SAMLAuthenticationProvider; let mockOptions: MockAuthenticationProviderOptions; - let mockUser: AuthenticatedUser; + let mockUser: ReturnType; let mockScopedClusterClient: ReturnType< typeof elasticsearchServiceMock.createScopedClusterClient >; diff --git a/x-pack/plugins/security/server/authentication/providers/token.test.ts b/x-pack/plugins/security/server/authentication/providers/token.test.ts index 4d802506071216..84a1649540267d 100644 --- a/x-pack/plugins/security/server/authentication/providers/token.test.ts +++ b/x-pack/plugins/security/server/authentication/providers/token.test.ts @@ -50,6 +50,7 @@ describe('TokenAuthenticationProvider', () => { const authorization = `Bearer ${tokenPair.accessToken}`; mockOptions.client.asInternalUser.security.getToken.mockResolvedValue( + // @ts-expect-error not full interface securityMock.createApiResponse({ body: { access_token: tokenPair.accessToken, diff --git a/x-pack/plugins/security/server/authentication/providers/token.ts b/x-pack/plugins/security/server/authentication/providers/token.ts index f202a0bd43fcff..43338a8f6400fb 100644 --- a/x-pack/plugins/security/server/authentication/providers/token.ts +++ b/x-pack/plugins/security/server/authentication/providers/token.ts @@ -72,16 +72,21 @@ export class TokenAuthenticationProvider extends BaseAuthenticationProvider { refresh_token: refreshToken, authentication: authenticationInfo, } = ( - await this.options.client.asInternalUser.security.getToken<{ - access_token: string; - refresh_token: string; - authentication: AuthenticationInfo; - }>({ body: { grant_type: 'password', username, password } }) + await this.options.client.asInternalUser.security.getToken({ + body: { + grant_type: 'password', + username, + password, + }, + }) ).body; this.logger.debug('Get token API request to Elasticsearch successful'); return AuthenticationResult.succeeded( - this.authenticationInfoToAuthenticatedUser(authenticationInfo), + this.authenticationInfoToAuthenticatedUser( + // @ts-expect-error @elastic/elasticsearch GetUserAccessTokenResponse declares authentication: string, but expected AuthenticatedUser + authenticationInfo as AuthenticationInfo + ), { authHeaders: { authorization: new HTTPAuthorizationHeader('Bearer', accessToken).toString(), diff --git a/x-pack/plugins/security/server/authentication/tokens.test.ts b/x-pack/plugins/security/server/authentication/tokens.test.ts index a6d52e355b1451..d02b368635e6fa 100644 --- a/x-pack/plugins/security/server/authentication/tokens.test.ts +++ b/x-pack/plugins/security/server/authentication/tokens.test.ts @@ -109,6 +109,9 @@ describe('Tokens', () => { access_token: tokenPair.accessToken, refresh_token: tokenPair.refreshToken, authentication: authenticationInfo, + type: 'Bearer', + expires_in: 1200, + scope: 'FULL', }, }) ); @@ -197,7 +200,14 @@ describe('Tokens', () => { const tokenPair = { accessToken: 'foo', refreshToken: 'bar' }; mockElasticsearchClient.security.invalidateToken.mockResolvedValue( - securityMock.createApiResponse({ body: { invalidated_tokens: 1 } }) + securityMock.createApiResponse({ + body: { + invalidated_tokens: 1, + previously_invalidated_tokens: 0, + error_count: 0, + error_details: [], + }, + }) ); await expect(tokens.invalidate(tokenPair)).resolves.toBe(undefined); @@ -215,7 +225,14 @@ describe('Tokens', () => { const tokenPair = { accessToken: 'foo' }; mockElasticsearchClient.security.invalidateToken.mockResolvedValue( - securityMock.createApiResponse({ body: { invalidated_tokens: 1 } }) + securityMock.createApiResponse({ + body: { + invalidated_tokens: 1, + previously_invalidated_tokens: 0, + error_count: 0, + error_details: [], + }, + }) ); await expect(tokens.invalidate(tokenPair)).resolves.toBe(undefined); @@ -230,7 +247,14 @@ describe('Tokens', () => { const tokenPair = { refreshToken: 'foo' }; mockElasticsearchClient.security.invalidateToken.mockResolvedValue( - securityMock.createApiResponse({ body: { invalidated_tokens: 1 } }) + securityMock.createApiResponse({ + body: { + invalidated_tokens: 1, + previously_invalidated_tokens: 0, + error_count: 0, + error_details: [], + }, + }) ); await expect(tokens.invalidate(tokenPair)).resolves.toBe(undefined); @@ -274,7 +298,14 @@ describe('Tokens', () => { const tokenPair = { accessToken: 'foo', refreshToken: 'bar' }; mockElasticsearchClient.security.invalidateToken.mockResolvedValue( - securityMock.createApiResponse({ body: { invalidated_tokens: 5 } }) + securityMock.createApiResponse({ + body: { + invalidated_tokens: 5, + previously_invalidated_tokens: 0, + error_count: 0, + error_details: [], + }, + }) ); await expect(tokens.invalidate(tokenPair)).resolves.toBe(undefined); diff --git a/x-pack/plugins/security/server/authentication/tokens.ts b/x-pack/plugins/security/server/authentication/tokens.ts index e3c4644775613a..8f6dd9275e59cd 100644 --- a/x-pack/plugins/security/server/authentication/tokens.ts +++ b/x-pack/plugins/security/server/authentication/tokens.ts @@ -60,16 +60,22 @@ export class Tokens { refresh_token: refreshToken, authentication: authenticationInfo, } = ( - await this.options.client.security.getToken<{ - access_token: string; - refresh_token: string; - authentication: AuthenticationInfo; - }>({ body: { grant_type: 'refresh_token', refresh_token: existingRefreshToken } }) + await this.options.client.security.getToken({ + body: { + grant_type: 'refresh_token', + refresh_token: existingRefreshToken, + }, + }) ).body; this.logger.debug('Access token has been successfully refreshed.'); - return { accessToken, refreshToken, authenticationInfo }; + return { + accessToken, + refreshToken, + // @ts-expect-error @elastic/elasticsearch decalred GetUserAccessTokenResponse.authentication: string + authenticationInfo: authenticationInfo as AuthenticationInfo, + }; } catch (err) { this.logger.debug(`Failed to refresh access token: ${err.message}`); diff --git a/x-pack/plugins/security/server/authorization/check_privileges.test.ts b/x-pack/plugins/security/server/authorization/check_privileges.test.ts index 2c1deed0f8c307..75c8229bb37d69 100644 --- a/x-pack/plugins/security/server/authorization/check_privileges.test.ts +++ b/x-pack/plugins/security/server/authorization/check_privileges.test.ts @@ -68,8 +68,8 @@ describe('#atSpace', () => { const expectedIndexPrivilegePayload = Object.entries( options.elasticsearchPrivileges?.index ?? {} - ).map(([names, indexPrivileges]) => ({ - names, + ).map(([name, indexPrivileges]) => ({ + names: [name], privileges: indexPrivileges, })); @@ -78,7 +78,7 @@ describe('#atSpace', () => { body: { cluster: options.elasticsearchPrivileges?.cluster, index: expectedIndexPrivilegePayload, - applications: [ + application: [ { application, resources: [`space:${options.spaceId}`], @@ -914,8 +914,8 @@ describe('#atSpaces', () => { const expectedIndexPrivilegePayload = Object.entries( options.elasticsearchPrivileges?.index ?? {} - ).map(([names, indexPrivileges]) => ({ - names, + ).map(([name, indexPrivileges]) => ({ + names: [name], privileges: indexPrivileges, })); @@ -924,7 +924,7 @@ describe('#atSpaces', () => { body: { cluster: options.elasticsearchPrivileges?.cluster, index: expectedIndexPrivilegePayload, - applications: [ + application: [ { application, resources: options.spaceIds.map((spaceId) => `space:${spaceId}`), @@ -2118,8 +2118,8 @@ describe('#globally', () => { const expectedIndexPrivilegePayload = Object.entries( options.elasticsearchPrivileges?.index ?? {} - ).map(([names, indexPrivileges]) => ({ - names, + ).map(([name, indexPrivileges]) => ({ + names: [name], privileges: indexPrivileges, })); @@ -2128,7 +2128,7 @@ describe('#globally', () => { body: { cluster: options.elasticsearchPrivileges?.cluster, index: expectedIndexPrivilegePayload, - applications: [ + application: [ { application, resources: [GLOBAL_RESOURCE], diff --git a/x-pack/plugins/security/server/authorization/check_privileges.ts b/x-pack/plugins/security/server/authorization/check_privileges.ts index 0fc11cddf9bbc8..3a35cf164ad85c 100644 --- a/x-pack/plugins/security/server/authorization/check_privileges.ts +++ b/x-pack/plugins/security/server/authorization/check_privileges.ts @@ -51,22 +51,22 @@ export function checkPrivilegesWithRequestFactory( const allApplicationPrivileges = uniq([actions.version, actions.login, ...kibanaPrivileges]); const clusterClient = await getClusterClient(); - const { body: hasPrivilegesResponse } = await clusterClient - .asScoped(request) - .asCurrentUser.security.hasPrivileges({ - body: { - cluster: privileges.elasticsearch?.cluster, - index: Object.entries(privileges.elasticsearch?.index ?? {}).map( - ([names, indexPrivileges]) => ({ - names, - privileges: indexPrivileges, - }) - ), - applications: [ - { application: applicationName, resources, privileges: allApplicationPrivileges }, - ], - }, - }); + const { body } = await clusterClient.asScoped(request).asCurrentUser.security.hasPrivileges({ + body: { + cluster: privileges.elasticsearch?.cluster, + index: Object.entries(privileges.elasticsearch?.index ?? {}).map( + ([name, indexPrivileges]) => ({ + names: [name], + privileges: indexPrivileges, + }) + ), + application: [ + { application: applicationName, resources, privileges: allApplicationPrivileges }, + ], + }, + }); + + const hasPrivilegesResponse: HasPrivilegesResponse = body; validateEsPrivilegeResponse( hasPrivilegesResponse, diff --git a/x-pack/plugins/security/server/routes/authorization/privileges/get_builtin.ts b/x-pack/plugins/security/server/routes/authorization/privileges/get_builtin.ts index e2539695d54eea..e3a586062ae4c8 100644 --- a/x-pack/plugins/security/server/routes/authorization/privileges/get_builtin.ts +++ b/x-pack/plugins/security/server/routes/authorization/privileges/get_builtin.ts @@ -6,7 +6,6 @@ */ import type { RouteDefinitionParams } from '../..'; -import type { BuiltinESPrivileges } from '../../../../common/model'; export function defineGetBuiltinPrivilegesRoutes({ router }: RouteDefinitionParams) { router.get( @@ -14,7 +13,7 @@ export function defineGetBuiltinPrivilegesRoutes({ router }: RouteDefinitionPara async (context, request, response) => { const { body: privileges, - } = await context.core.elasticsearch.client.asCurrentUser.security.getBuiltinPrivileges(); + } = await context.core.elasticsearch.client.asCurrentUser.security.getBuiltinPrivileges(); // Exclude the `none` privilege, as it doesn't make sense as an option within the Kibana UI privileges.cluster = privileges.cluster.filter((privilege) => privilege !== 'none'); diff --git a/x-pack/plugins/security/server/routes/authorization/roles/get.ts b/x-pack/plugins/security/server/routes/authorization/roles/get.ts index 3bfc5e5d4dda39..01d32f7fb82339 100644 --- a/x-pack/plugins/security/server/routes/authorization/roles/get.ts +++ b/x-pack/plugins/security/server/routes/authorization/roles/get.ts @@ -10,7 +10,6 @@ import { schema } from '@kbn/config-schema'; import type { RouteDefinitionParams } from '../..'; import { wrapIntoCustomErrorResponse } from '../../../errors'; import { createLicensedRouteHandler } from '../../licensed_route_handler'; -import type { ElasticsearchRole } from './model'; import { transformElasticsearchRoleToRole } from './model'; export function defineGetRolesRoutes({ router, authz }: RouteDefinitionParams) { @@ -25,14 +24,15 @@ export function defineGetRolesRoutes({ router, authz }: RouteDefinitionParams) { try { const { body: elasticsearchRoles, - } = await context.core.elasticsearch.client.asCurrentUser.security.getRole< - Record - >({ name: request.params.name }); + } = await context.core.elasticsearch.client.asCurrentUser.security.getRole({ + name: request.params.name, + }); const elasticsearchRole = elasticsearchRoles[request.params.name]; if (elasticsearchRole) { return response.ok({ body: transformElasticsearchRoleToRole( + // @ts-expect-error @elastic/elasticsearch `XPackRole` type doesn't define `applications` and `transient_metadata`. elasticsearchRole, request.params.name, authz.applicationName diff --git a/x-pack/plugins/security/server/routes/authorization/roles/get_all.ts b/x-pack/plugins/security/server/routes/authorization/roles/get_all.ts index 2994afd40f880e..4d458be4e332fb 100644 --- a/x-pack/plugins/security/server/routes/authorization/roles/get_all.ts +++ b/x-pack/plugins/security/server/routes/authorization/roles/get_all.ts @@ -26,7 +26,12 @@ export function defineGetAllRolesRoutes({ router, authz }: RouteDefinitionParams return response.ok({ body: Object.entries(elasticsearchRoles) .map(([roleName, elasticsearchRole]) => - transformElasticsearchRoleToRole(elasticsearchRole, roleName, authz.applicationName) + transformElasticsearchRoleToRole( + // @ts-expect-error @elastic/elasticsearch `XPackRole` type doesn't define `applications` and `transient_metadata`. + elasticsearchRole, + roleName, + authz.applicationName + ) ) .sort((roleA, roleB) => { if (roleA.name < roleB.name) { diff --git a/x-pack/plugins/security/server/routes/authorization/roles/model/elasticsearch_role.ts b/x-pack/plugins/security/server/routes/authorization/roles/model/elasticsearch_role.ts index f033b668050671..74a035cdd0cb62 100644 --- a/x-pack/plugins/security/server/routes/authorization/roles/model/elasticsearch_role.ts +++ b/x-pack/plugins/security/server/routes/authorization/roles/model/elasticsearch_role.ts @@ -25,7 +25,7 @@ export type ElasticsearchRole = Pick, name: string, application: string ): Role { diff --git a/x-pack/plugins/security/server/routes/authorization/roles/put.ts b/x-pack/plugins/security/server/routes/authorization/roles/put.ts index aefcc0c72c6db4..09bcb6b8c505ce 100644 --- a/x-pack/plugins/security/server/routes/authorization/roles/put.ts +++ b/x-pack/plugins/security/server/routes/authorization/roles/put.ts @@ -12,7 +12,6 @@ import type { KibanaFeature } from '../../../../../features/common'; import { wrapIntoCustomErrorResponse } from '../../../errors'; import type { RouteDefinitionParams } from '../../index'; import { createLicensedRouteHandler } from '../../licensed_route_handler'; -import type { ElasticsearchRole } from './model'; import { getPutPayloadSchema, transformPutPayloadToElasticsearchRole } from './model'; const roleGrantsSubFeaturePrivileges = ( @@ -65,13 +64,15 @@ export function definePutRolesRoutes({ try { const { body: rawRoles, - } = await context.core.elasticsearch.client.asCurrentUser.security.getRole< - Record - >({ name: request.params.name }, { ignore: [404] }); + } = await context.core.elasticsearch.client.asCurrentUser.security.getRole( + { name: request.params.name }, + { ignore: [404] } + ); const body = transformPutPayloadToElasticsearchRole( request.body, authz.applicationName, + // @ts-expect-error @elastic/elasticsearch `XPackRole` type doesn't define `applications`. rawRoles[name] ? rawRoles[name].applications : [] ); @@ -79,6 +80,7 @@ export function definePutRolesRoutes({ getFeatures(), context.core.elasticsearch.client.asCurrentUser.security.putRole({ name: request.params.name, + // @ts-expect-error RoleIndexPrivilege is not compatible. grant is required in IndicesPrivileges.field_security body, }), ]); diff --git a/x-pack/plugins/security/server/routes/indices/get_fields.ts b/x-pack/plugins/security/server/routes/indices/get_fields.ts index 3ed7493ea1f0ea..63704682e36351 100644 --- a/x-pack/plugins/security/server/routes/indices/get_fields.ts +++ b/x-pack/plugins/security/server/routes/indices/get_fields.ts @@ -10,20 +10,6 @@ import { schema } from '@kbn/config-schema'; import { wrapIntoCustomErrorResponse } from '../../errors'; import type { RouteDefinitionParams } from '../index'; -interface FieldMappingResponse { - [indexName: string]: { - mappings: { - [fieldName: string]: { - mapping: { - [fieldName: string]: { - type: string; - }; - }; - }; - }; - }; -} - export function defineGetFieldsRoutes({ router }: RouteDefinitionParams) { router.get( { @@ -34,14 +20,12 @@ export function defineGetFieldsRoutes({ router }: RouteDefinitionParams) { try { const { body: indexMappings, - } = await context.core.elasticsearch.client.asCurrentUser.indices.getFieldMapping( - { - index: request.params.query, - fields: '*', - allow_no_indices: false, - include_defaults: true, - } - ); + } = await context.core.elasticsearch.client.asCurrentUser.indices.getFieldMapping({ + index: request.params.query, + fields: '*', + allow_no_indices: false, + include_defaults: true, + }); // The flow is the following (see response format at https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html): // 1. Iterate over all matched indices. @@ -52,7 +36,13 @@ export function defineGetFieldsRoutes({ router }: RouteDefinitionParams) { new Set( Object.values(indexMappings).flatMap((indexMapping) => { return Object.keys(indexMapping.mappings).filter((fieldName) => { - const mappingValues = Object.values(indexMapping.mappings[fieldName].mapping); + const mappingValues = Object.values( + // `FieldMapping` type from `TypeFieldMappings` --> `GetFieldMappingResponse` is not correct and + // doesn't have any properties. + (indexMapping.mappings[fieldName] as { + mapping: Record; + }).mapping + ); const hasMapping = mappingValues.length > 0; const isRuntimeField = hasMapping && mappingValues[0]?.type === 'runtime'; diff --git a/x-pack/plugins/security/server/routes/role_mapping/get.ts b/x-pack/plugins/security/server/routes/role_mapping/get.ts index d060825a989d57..67cd8975b76ebb 100644 --- a/x-pack/plugins/security/server/routes/role_mapping/get.ts +++ b/x-pack/plugins/security/server/routes/role_mapping/get.ts @@ -12,10 +12,6 @@ import type { RoleMapping } from '../../../common/model'; import { wrapError } from '../../errors'; import { createLicensedRouteHandler } from '../licensed_route_handler'; -interface RoleMappingsResponse { - [roleMappingName: string]: Omit; -} - export function defineRoleMappingGetRoutes(params: RouteDefinitionParams) { const { logger, router } = params; @@ -32,7 +28,7 @@ export function defineRoleMappingGetRoutes(params: RouteDefinitionParams) { const expectSingleEntity = typeof request.params.name === 'string'; try { - const roleMappingsResponse = await context.core.elasticsearch.client.asCurrentUser.security.getRoleMapping( + const roleMappingsResponse = await context.core.elasticsearch.client.asCurrentUser.security.getRoleMapping( { name: request.params.name } ); @@ -40,7 +36,8 @@ export function defineRoleMappingGetRoutes(params: RouteDefinitionParams) { return { name, ...mapping, - role_templates: (mapping.role_templates || []).map((entry) => { + // @ts-expect-error @elastic/elasticsearch `XPackRoleMapping` type doesn't define `role_templates` property. + role_templates: (mapping.role_templates || []).map((entry: RoleTemplate) => { return { ...entry, template: tryParseRoleTemplate(entry.template as string), diff --git a/x-pack/plugins/security/server/routes/users/get.ts b/x-pack/plugins/security/server/routes/users/get.ts index 28165ef32356d3..81502044ef94d2 100644 --- a/x-pack/plugins/security/server/routes/users/get.ts +++ b/x-pack/plugins/security/server/routes/users/get.ts @@ -24,9 +24,7 @@ export function defineGetUserRoutes({ router }: RouteDefinitionParams) { const username = request.params.username; const { body: users, - } = await context.core.elasticsearch.client.asCurrentUser.security.getUser< - Record - >({ + } = await context.core.elasticsearch.client.asCurrentUser.security.getUser({ username, }); diff --git a/x-pack/plugins/security/server/session_management/session_index.test.ts b/x-pack/plugins/security/server/session_management/session_index.test.ts index b5b4f644389020..11fb4ca27f5902 100644 --- a/x-pack/plugins/security/server/session_management/session_index.test.ts +++ b/x-pack/plugins/security/server/session_management/session_index.test.ts @@ -41,7 +41,7 @@ describe('Session index', () => { name: indexTemplateName, }); expect(mockElasticsearchClient.indices.exists).toHaveBeenCalledWith({ - index: getSessionIndexTemplate(indexName).index_patterns, + index: getSessionIndexTemplate(indexName).index_patterns[0], }); } @@ -93,7 +93,7 @@ describe('Session index', () => { body: expectedIndexTemplate, }); expect(mockElasticsearchClient.indices.create).toHaveBeenCalledWith({ - index: expectedIndexTemplate.index_patterns, + index: expectedIndexTemplate.index_patterns[0], }); }); @@ -126,7 +126,7 @@ describe('Session index', () => { assertExistenceChecksPerformed(); expect(mockElasticsearchClient.indices.create).toHaveBeenCalledWith({ - index: getSessionIndexTemplate(indexName).index_patterns, + index: getSessionIndexTemplate(indexName).index_patterns[0], }); }); @@ -166,7 +166,7 @@ describe('Session index', () => { const now = 123456; beforeEach(() => { mockElasticsearchClient.deleteByQuery.mockResolvedValue( - securityMock.createApiResponse({ body: {} }) + securityMock.createApiResponse({ body: {} as any }) ); jest.spyOn(Date, 'now').mockImplementation(() => now); }); @@ -600,7 +600,10 @@ describe('Session index', () => { it('returns `null` if index is not found', async () => { mockElasticsearchClient.get.mockResolvedValue( - securityMock.createApiResponse({ statusCode: 404, body: { status: 404 } }) + securityMock.createApiResponse({ + statusCode: 404, + body: { _index: 'my-index', _type: '_doc', _id: '0', found: false }, + }) ); await expect(sessionIndex.get('some-sid')).resolves.toBeNull(); @@ -608,7 +611,10 @@ describe('Session index', () => { it('returns `null` if session index value document is not found', async () => { mockElasticsearchClient.get.mockResolvedValue( - securityMock.createApiResponse({ body: { status: 200, found: false } }) + securityMock.createApiResponse({ + statusCode: 200, + body: { _index: 'my-index', _type: '_doc', _id: '0', found: false }, + }) ); await expect(sessionIndex.get('some-sid')).resolves.toBeNull(); @@ -625,9 +631,12 @@ describe('Session index', () => { mockElasticsearchClient.get.mockResolvedValue( securityMock.createApiResponse({ + statusCode: 200, body: { found: true, - status: 200, + _index: 'my-index', + _type: '_doc', + _id: '0', _source: indexDocumentSource, _primary_term: 1, _seq_no: 456, @@ -670,7 +679,17 @@ describe('Session index', () => { it('properly stores session value in the index', async () => { mockElasticsearchClient.create.mockResolvedValue( - securityMock.createApiResponse({ body: { _primary_term: 321, _seq_no: 654 } }) + securityMock.createApiResponse({ + body: { + _shards: { total: 1, failed: 0, successful: 1, skipped: 0 }, + _index: 'my-index', + _id: 'W0tpsmIBdwcYyG50zbta', + _version: 1, + _primary_term: 321, + _seq_no: 654, + result: 'created', + }, + }) ); const sid = 'some-long-sid'; @@ -708,7 +727,7 @@ describe('Session index', () => { await expect(sessionIndex.update(sessionIndexMock.createValue())).rejects.toBe(failureReason); }); - it('refetches latest session value if update fails due to conflict', async () => { + it('re-fetches latest session value if update fails due to conflict', async () => { const latestSessionValue = { usernameHash: 'some-username-hash', provider: { type: 'basic', name: 'basic1' }, @@ -719,17 +738,31 @@ describe('Session index', () => { mockElasticsearchClient.get.mockResolvedValue( securityMock.createApiResponse({ + statusCode: 200, body: { - found: true, - status: 200, + _index: 'my-index', + _type: '_doc', + _id: '0', _source: latestSessionValue, _primary_term: 321, _seq_no: 654, + found: true, }, }) ); mockElasticsearchClient.index.mockResolvedValue( - securityMock.createApiResponse({ statusCode: 409, body: { status: 409 } }) + securityMock.createApiResponse({ + statusCode: 409, + body: { + _shards: { total: 1, failed: 0, successful: 1, skipped: 0 }, + _index: 'my-index', + _id: 'W0tpsmIBdwcYyG50zbta', + _version: 1, + _primary_term: 321, + _seq_no: 654, + result: 'updated', + }, + }) ); const sid = 'some-long-sid'; @@ -763,7 +796,18 @@ describe('Session index', () => { it('properly stores session value in the index', async () => { mockElasticsearchClient.index.mockResolvedValue( - securityMock.createApiResponse({ body: { _primary_term: 321, _seq_no: 654, status: 200 } }) + securityMock.createApiResponse({ + statusCode: 200, + body: { + _shards: { total: 1, failed: 0, successful: 1, skipped: 0 }, + _index: 'my-index', + _id: 'W0tpsmIBdwcYyG50zbta', + _version: 1, + _primary_term: 321, + _seq_no: 654, + result: 'created', + }, + }) ); const sid = 'some-long-sid'; diff --git a/x-pack/plugins/security/server/session_management/session_index.ts b/x-pack/plugins/security/server/session_management/session_index.ts index 1b5c820ec47106..d7a4c3e2520bf2 100644 --- a/x-pack/plugins/security/server/session_management/session_index.ts +++ b/x-pack/plugins/security/server/session_management/session_index.ts @@ -39,7 +39,7 @@ const SESSION_INDEX_TEMPLATE_VERSION = 1; */ export function getSessionIndexTemplate(indexName: string) { return Object.freeze({ - index_patterns: indexName, + index_patterns: [indexName], order: 1000, settings: { index: { @@ -52,7 +52,7 @@ export function getSessionIndexTemplate(indexName: string) { }, }, mappings: { - dynamic: 'strict', + dynamic: 'strict' as 'strict', properties: { usernameHash: { type: 'keyword' }, provider: { properties: { name: { type: 'keyword' }, type: { type: 'keyword' } } }, @@ -151,13 +151,16 @@ export class SessionIndex { */ async get(sid: string) { try { - const { body: response } = await this.options.elasticsearchClient.get( + const { + body: response, + statusCode, + } = await this.options.elasticsearchClient.get( { id: sid, index: this.indexName }, { ignore: [404] } ); const docNotFound = response.found === false; - const indexNotFound = response.status === 404; + const indexNotFound = statusCode === 404; if (docNotFound || indexNotFound) { this.options.logger.debug('Cannot find session value with the specified ID.'); return null; @@ -215,7 +218,7 @@ export class SessionIndex { async update(sessionValue: Readonly) { const { sid, metadata, ...sessionValueToStore } = sessionValue; try { - const { body: response } = await this.options.elasticsearchClient.index( + const { body: response, statusCode } = await this.options.elasticsearchClient.index( { id: sid, index: this.indexName, @@ -230,7 +233,7 @@ export class SessionIndex { // We don't want to override changes that were made after we fetched session value or // re-create it if has been deleted already. If we detect such a case we discard changes and // return latest copy of the session value instead or `null` if doesn't exist anymore. - const sessionIndexValueUpdateConflict = response.status === 409; + const sessionIndexValueUpdateConflict = statusCode === 409; if (sessionIndexValueUpdateConflict) { this.options.logger.debug( 'Cannot update session value due to conflict, session either does not exist or was already updated.' @@ -457,7 +460,7 @@ export class SessionIndex { { ignore: [409, 404] } ); - if (response.deleted > 0) { + if (response.deleted! > 0) { this.options.logger.debug( `Cleaned up ${response.deleted} invalid or expired session values.` ); diff --git a/x-pack/plugins/security_solution/common/search_strategy/common/index.ts b/x-pack/plugins/security_solution/common/search_strategy/common/index.ts index 05c47605b4951c..e27e9b5173fd59 100644 --- a/x-pack/plugins/security_solution/common/search_strategy/common/index.ts +++ b/x-pack/plugins/security_solution/common/search_strategy/common/index.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import { IEsSearchResponse } from '../../../../../../src/plugins/data/common'; export type Maybe = T | null; @@ -70,10 +70,7 @@ export interface PaginationInputPaginated { querySize: number; } -export interface DocValueFields { - field: string; - format?: string | null; -} +export type DocValueFields = estypes.DocValueField; export interface Explanation { value: number; diff --git a/x-pack/plugins/security_solution/common/search_strategy/security_solution/index.ts b/x-pack/plugins/security_solution/common/search_strategy/security_solution/index.ts index 319933be5c79e5..2160ed6170e29e 100644 --- a/x-pack/plugins/security_solution/common/search_strategy/security_solution/index.ts +++ b/x-pack/plugins/security_solution/common/search_strategy/security_solution/index.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import { IEsSearchRequest } from '../../../../../../src/plugins/data/common'; import { ESQuery } from '../../typed_json'; import { @@ -64,13 +64,7 @@ import { MatrixHistogramRequestOptions, MatrixHistogramStrategyResponse, } from './matrix_histogram'; -import { - DocValueFields, - TimerangeInput, - SortField, - PaginationInput, - PaginationInputPaginated, -} from '../common'; +import { TimerangeInput, SortField, PaginationInput, PaginationInputPaginated } from '../common'; export * from './hosts'; export * from './matrix_histogram'; @@ -87,7 +81,7 @@ export interface RequestBasicOptions extends IEsSearchRequest { timerange: TimerangeInput; filterQuery: ESQuery | string | undefined; defaultIndex: string[]; - docValueFields?: DocValueFields[]; + docValueFields?: estypes.DocValueField[]; factoryQueryType?: FactoryQueryTypes; } diff --git a/x-pack/plugins/security_solution/public/common/containers/source/index.tsx b/x-pack/plugins/security_solution/public/common/containers/source/index.tsx index 6daced08be2823..f66b060b166bf8 100644 --- a/x-pack/plugins/security_solution/public/common/containers/source/index.tsx +++ b/x-pack/plugins/security_solution/public/common/containers/source/index.tsx @@ -94,7 +94,7 @@ export const getDocValueFields = memoizeOne( ...accumulator, { field: field.name, - format: field.format, + format: field.format ? field.format : undefined, }, ]; } diff --git a/x-pack/plugins/security_solution/server/endpoint/routes/resolver/entity.ts b/x-pack/plugins/security_solution/server/endpoint/routes/resolver/entity.ts index cc8b99875758d1..a266cff73344f4 100644 --- a/x-pack/plugins/security_solution/server/endpoint/routes/resolver/entity.ts +++ b/x-pack/plugins/security_solution/server/endpoint/routes/resolver/entity.ts @@ -6,9 +6,8 @@ */ import _ from 'lodash'; -import { RequestHandler, SearchResponse } from 'kibana/server'; +import { RequestHandler } from 'kibana/server'; import { TypeOf } from '@kbn/config-schema'; -import { ApiResponse } from '@elastic/elasticsearch'; import { validateEntities } from '../../../../common/endpoint/schema/resolver'; import { ResolverEntityIndex, ResolverSchema } from '../../../../common/endpoint/types'; @@ -88,9 +87,7 @@ export function handleEntities(): RequestHandler - > = await context.core.elasticsearch.client.asCurrentUser.search({ + const queryResponse = await context.core.elasticsearch.client.asCurrentUser.search({ ignore_unavailable: true, index: indices, body: { @@ -102,6 +99,7 @@ export function handleEntities(): RequestHandler { const parsedFilters = EventsQuery.buildFilters(filter); - const response: ApiResponse< - SearchResponse - > = await client.asCurrentUser.search(this.buildSearch(parsedFilters)); + const response = await client.asCurrentUser.search( + this.buildSearch(parsedFilters) + ); + // @ts-expect-error @elastic/elasticsearch _source is optional return response.body.hits.hits.map((hit) => hit._source); } } diff --git a/x-pack/plugins/security_solution/server/endpoint/routes/resolver/tree/queries/descendants.ts b/x-pack/plugins/security_solution/server/endpoint/routes/resolver/tree/queries/descendants.ts index 8edfa81efbca66..bf9b3ce6aa8f3a 100644 --- a/x-pack/plugins/security_solution/server/endpoint/routes/resolver/tree/queries/descendants.ts +++ b/x-pack/plugins/security_solution/server/endpoint/routes/resolver/tree/queries/descendants.ts @@ -5,8 +5,7 @@ * 2.0. */ -import { SearchResponse } from 'elasticsearch'; -import { ApiResponse } from '@elastic/elasticsearch'; +import type { ApiResponse, estypes } from '@elastic/elasticsearch'; import { IScopedClusterClient } from 'src/core/server'; import { FieldsObject, ResolverSchema } from '../../../../../../common/endpoint/types'; import { JsonObject, JsonValue } from '../../../../../../../../../src/plugins/kibana_utils/common'; @@ -198,7 +197,7 @@ export class DescendantsQuery { return []; } - let response: ApiResponse>; + let response: ApiResponse>; if (this.schema.ancestry) { response = await client.asCurrentUser.search({ body: this.queryWithAncestryArray(validNodes, this.schema.ancestry, limit), @@ -219,6 +218,7 @@ export class DescendantsQuery { * * So the schema fields are flattened ('process.parent.entity_id') */ + // @ts-expect-error @elastic/elasticsearch _source is optional return response.body.hits.hits.map((hit) => hit.fields); } } diff --git a/x-pack/plugins/security_solution/server/endpoint/routes/resolver/tree/queries/lifecycle.ts b/x-pack/plugins/security_solution/server/endpoint/routes/resolver/tree/queries/lifecycle.ts index d04f0d8ee3bc99..f9780d1469756e 100644 --- a/x-pack/plugins/security_solution/server/endpoint/routes/resolver/tree/queries/lifecycle.ts +++ b/x-pack/plugins/security_solution/server/endpoint/routes/resolver/tree/queries/lifecycle.ts @@ -5,8 +5,6 @@ * 2.0. */ -import { SearchResponse } from 'elasticsearch'; -import { ApiResponse } from '@elastic/elasticsearch'; import { IScopedClusterClient } from 'src/core/server'; import { FieldsObject, ResolverSchema } from '../../../../../../common/endpoint/types'; import { JsonObject, JsonValue } from '../../../../../../../../../src/plugins/kibana_utils/common'; @@ -93,7 +91,7 @@ export class LifecycleQuery { return []; } - const response: ApiResponse> = await client.asCurrentUser.search({ + const response = await client.asCurrentUser.search({ body: this.query(validNodes), index: this.indexPatterns, }); @@ -106,6 +104,7 @@ export class LifecycleQuery { * * So the schema fields are flattened ('process.parent.entity_id') */ + // @ts-expect-error @elastic/elasticsearch _source is optional return response.body.hits.hits.map((hit) => hit.fields); } } diff --git a/x-pack/plugins/security_solution/server/endpoint/routes/resolver/tree/queries/stats.ts b/x-pack/plugins/security_solution/server/endpoint/routes/resolver/tree/queries/stats.ts index 5d9db0c148d424..24c97ad88b26ae 100644 --- a/x-pack/plugins/security_solution/server/endpoint/routes/resolver/tree/queries/stats.ts +++ b/x-pack/plugins/security_solution/server/endpoint/routes/resolver/tree/queries/stats.ts @@ -5,8 +5,6 @@ * 2.0. */ -import { SearchResponse } from 'elasticsearch'; -import { ApiResponse } from '@elastic/elasticsearch'; import { IScopedClusterClient } from 'src/core/server'; import { JsonObject } from '../../../../../../../../../src/plugins/kibana_utils/common'; import { EventStats, ResolverSchema } from '../../../../../../common/endpoint/types'; @@ -125,11 +123,12 @@ export class StatsQuery { } // leaving unknown here because we don't actually need the hits part of the body - const response: ApiResponse> = await client.asCurrentUser.search({ + const response = await client.asCurrentUser.search({ body: this.query(nodes), index: this.indexPatterns, }); + // @ts-expect-error @elastic/elasticsearch no way to declare a type for aggregation in the search response return response.body.aggregations?.ids?.buckets.reduce( (cummulative: Record, bucket: CategoriesAgg) => ({ ...cummulative, diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/index/delete_all_index.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/index/delete_all_index.ts index 98a8f8c28d30de..48bbbdcbf3a48d 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/index/delete_all_index.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/index/delete_all_index.ts @@ -27,6 +27,7 @@ export const deleteAllIndex = async ( { ignore: [404] } ); + // @ts-expect-error status doesn't exist on response if (resp.status === 404) { return true; } diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/index/get_index_exists.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/index/get_index_exists.test.ts index 488ba0dab0b973..abe587ec825c03 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/index/get_index_exists.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/index/get_index_exists.test.ts @@ -21,6 +21,7 @@ describe('get_index_exists', () => { test('it should return a true if you have _shards', async () => { const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser; esClient.search.mockReturnValue( + // @ts-expect-error not full interface elasticsearchClientMock.createSuccessTransportRequestPromise({ _shards: { total: 1 } }) ); const indexExists = await getIndexExists(esClient, 'some-index'); @@ -30,6 +31,7 @@ describe('get_index_exists', () => { test('it should return a false if you do NOT have _shards', async () => { const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser; esClient.search.mockReturnValue( + // @ts-expect-error not full interface elasticsearchClientMock.createSuccessTransportRequestPromise({ _shards: { total: 0 } }) ); const indexExists = await getIndexExists(esClient, 'some-index'); diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/index/get_index_exists.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/index/get_index_exists.ts index b86b58897ee621..cc7f22064572c1 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/index/get_index_exists.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/index/get_index_exists.ts @@ -15,8 +15,10 @@ export const getIndexExists = async ( const { body: response } = await esClient.search({ index, size: 0, - terminate_after: 1, allow_no_indices: true, + body: { + terminate_after: 1, + }, }); return response._shards.total > 0; } catch (err) { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/create_migration.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/create_migration.ts index d1d43ded321019..bf091ef2508afc 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/create_migration.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/create_migration.ts @@ -73,7 +73,7 @@ export const createMigration = async ({ return { destinationIndex: migrationIndex, sourceIndex: index, - taskId: response.body.task, + taskId: String(response.body.task!), version, }; }; diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_index_versions_by_index.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_index_versions_by_index.test.ts index 32b3ccbf17b57d..8e99cb32390e1f 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_index_versions_by_index.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_index_versions_by_index.test.ts @@ -43,9 +43,9 @@ describe('getIndexVersionsByIndex', () => { }); it('properly transforms the response', async () => { - // @ts-expect-error mocking only what we need esClient.indices.getMapping.mockResolvedValue({ body: { + // @ts-expect-error mocking only what we need index1: { mappings: { _meta: { version: 3 } } }, }, }); diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_signal_versions_by_index.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_signal_versions_by_index.test.ts index 24d8fab9c50de9..2cdfa2c13e7b59 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_signal_versions_by_index.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_signal_versions_by_index.test.ts @@ -16,8 +16,8 @@ describe('getSignalVersionsByIndex', () => { }); it('properly transforms the elasticsearch aggregation', async () => { - // @ts-expect-error mocking only what we need esClient.search.mockResolvedValueOnce({ + // @ts-expect-error mocking only what we need body: { aggregations: { signals_indices: { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_signal_versions_by_index.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_signal_versions_by_index.ts index ea064a93978796..784164e430ff0f 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_signal_versions_by_index.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_signal_versions_by_index.ts @@ -50,7 +50,7 @@ export const getSignalVersionsByIndex = async ({ esClient: ElasticsearchClient; index: string[]; }): Promise => { - const { body } = await esClient.search({ + const response = await esClient.search({ index, size: 0, body: { @@ -71,6 +71,9 @@ export const getSignalVersionsByIndex = async ({ }, }, }); + + // @ts-expect-error @elastic/elasticsearch no way to declare a type for aggregation in the search response + const body = response.body as SignalVersionsAggResponse; const indexBuckets = body.aggregations.signals_indices.buckets; return index.reduce((agg, _index) => { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_signals_indices_in_range.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_signals_indices_in_range.ts index 3135bfec4e0e75..3c9132fc81279a 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_signals_indices_in_range.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/migrations/get_signals_indices_in_range.ts @@ -42,7 +42,7 @@ export const getSignalsIndicesInRange = async ({ return []; } - const response = await esClient.search({ + const response = await esClient.search({ index, body: { aggs: { @@ -60,6 +60,7 @@ export const getSignalsIndicesInRange = async ({ '@timestamp': { gte: from, lte: 'now', + // @ts-expect-error format doesn't exist in RangeQuery format: 'strict_date_optional_time', }, }, @@ -71,5 +72,7 @@ export const getSignalsIndicesInRange = async ({ }, }); - return response.body.aggregations.indexes.buckets.map((bucket) => bucket.key); + // @ts-expect-error @elastic/elasticsearch no way to declare a type for aggregation in the search response + const body = response.body as IndexesResponse; + return body.aggregations.indexes.buckets.map((bucket) => bucket.key); }; diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/get_signals.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/get_signals.ts index b411ac2c69ef28..398438234ed71d 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/get_signals.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/get_signals.ts @@ -6,7 +6,7 @@ */ import { ElasticsearchClient } from 'kibana/server'; -import { SignalSearchResponse } from '../signals/types'; +import type { SignalSearchResponse, SignalSource } from '../signals/types'; import { buildSignalsSearchQuery } from './build_signals_query'; interface GetSignalsParams { @@ -38,7 +38,7 @@ export const getSignals = async ({ size, }); - const { body: result } = await esClient.search(query); + const { body: result } = await esClient.search(query); return result; }; diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/rules_notification_alert_type.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/rules_notification_alert_type.ts index a40cb998eb408f..799fb3814f1f0d 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/rules_notification_alert_type.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/notifications/rules_notification_alert_type.ts @@ -91,6 +91,7 @@ export const rulesNotificationAlertType = ({ signalsCount, resultsLink, ruleParams, + // @ts-expect-error @elastic/elasticsearch _source is optional signals, }); } diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/index/get_index_version.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/index/get_index_version.ts index 5c626cbe33ac16..b333ef999a6aef 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/index/get_index_version.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/index/get_index_version.ts @@ -5,26 +5,15 @@ * 2.0. */ -import { ApiResponse } from '@elastic/elasticsearch'; import { get } from 'lodash'; import { ElasticsearchClient } from '../../../../../../../../src/core/server'; import { readIndex } from '../../index/read_index'; -interface IndicesAliasResponse { - [index: string]: IndexAliasResponse; -} - -interface IndexAliasResponse { - aliases: { - [aliasName: string]: Record; - }; -} - export const getIndexVersion = async ( esClient: ElasticsearchClient, index: string ): Promise => { - const { body: indexAlias }: ApiResponse = await esClient.indices.getAlias({ + const { body: indexAlias } = await esClient.indices.getAlias({ index, }); const writeIndex = Object.keys(indexAlias).find( diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/__mocks__/es_results.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/__mocks__/es_results.ts index 4106d7532f7bb1..8c9b19a0929d2e 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/__mocks__/es_results.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/__mocks__/es_results.ts @@ -6,7 +6,7 @@ */ import { set } from '@elastic/safer-lodash-set'; -import { +import type { SignalSourceHit, SignalSearchResponse, BulkResponse, @@ -261,7 +261,9 @@ export const sampleWrappedSignalHit = (): WrappedSignalHit => { export const sampleDocWithAncestors = (): SignalSearchResponse => { const sampleDoc = sampleDocNoSortId(); delete sampleDoc.sort; + // @ts-expect-error @elastic/elasticsearch _source is optional delete sampleDoc._source.source; + // @ts-expect-error @elastic/elasticsearch _source is optional sampleDoc._source.signal = { parent: { id: 'd5e8eb51-a6a0-456d-8a15-4b79bfec3d71', diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_bulk_body.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_bulk_body.test.ts index 362c368881b37f..708aefc4d86147 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_bulk_body.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_bulk_body.test.ts @@ -33,6 +33,7 @@ describe('buildBulkBody', () => { test('bulk body builds well-defined body', () => { const sampleParams = sampleRuleAlertParams(); const doc = sampleDocNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional delete doc._source.source; const fakeSignalSourceHit = buildBulkBody({ doc, @@ -143,6 +144,7 @@ describe('buildBulkBody', () => { const baseDoc = sampleDocNoSortId(); const doc: SignalSourceHit = { ...baseDoc, + // @ts-expect-error @elastic/elasticsearch _source is optional _source: { ...baseDoc._source, threshold_result: { @@ -155,6 +157,7 @@ describe('buildBulkBody', () => { }, }, }; + // @ts-expect-error @elastic/elasticsearch _source is optional delete doc._source.source; const fakeSignalSourceHit = buildBulkBody({ doc, @@ -271,7 +274,9 @@ describe('buildBulkBody', () => { test('bulk body builds original_event if it exists on the event to begin with', () => { const sampleParams = sampleRuleAlertParams(); const doc = sampleDocNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional delete doc._source.source; + // @ts-expect-error @elastic/elasticsearch _source is optional doc._source.event = { action: 'socket_opened', module: 'system', @@ -384,7 +389,9 @@ describe('buildBulkBody', () => { test('bulk body builds original_event if it exists on the event to begin with but no kind information', () => { const sampleParams = sampleRuleAlertParams(); const doc = sampleDocNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional delete doc._source.source; + // @ts-expect-error @elastic/elasticsearch _source is optional doc._source.event = { action: 'socket_opened', module: 'system', @@ -495,7 +502,9 @@ describe('buildBulkBody', () => { test('bulk body builds original_event if it exists on the event to begin with with only kind information', () => { const sampleParams = sampleRuleAlertParams(); const doc = sampleDocNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional delete doc._source.source; + // @ts-expect-error @elastic/elasticsearch _source is optional doc._source.event = { kind: 'event', }; @@ -599,6 +608,7 @@ describe('buildBulkBody', () => { test('bulk body builds "original_signal" if it exists already as a numeric', () => { const sampleParams = sampleRuleAlertParams(); const sampleDoc = sampleDocNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional delete sampleDoc._source.source; const doc = ({ ...sampleDoc, @@ -702,6 +712,7 @@ describe('buildBulkBody', () => { test('bulk body builds "original_signal" if it exists already as an object', () => { const sampleParams = sampleRuleAlertParams(); const sampleDoc = sampleDocNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional delete sampleDoc._source.source; const doc = ({ ...sampleDoc, @@ -1053,6 +1064,7 @@ describe('buildSignalFromSequence', () => { describe('buildSignalFromEvent', () => { test('builds a basic signal from a single event', () => { const ancestor = sampleDocWithAncestors().hits.hits[0]; + // @ts-expect-error @elastic/elasticsearch _source is optional delete ancestor._source.source; const ruleSO = sampleRuleSO(); const signal = buildSignalFromEvent(ancestor, ruleSO, true); diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_bulk_body.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_bulk_body.ts index fb72bf9c59b4ec..3a6cbf5ccd34bd 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_bulk_body.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_bulk_body.ts @@ -73,6 +73,7 @@ export const buildBulkBody = ({ ...buildSignal([doc], rule), ...additionalSignalFields(doc), }; + // @ts-expect-error @elastic/elasticsearch _source is optional delete doc._source.threshold_result; const event = buildEventTypeSignal(doc); const signalHit: SignalHit = { @@ -163,7 +164,8 @@ export const buildSignalFromEvent = ( applyOverrides: boolean ): SignalHit => { const rule = applyOverrides - ? buildRuleWithOverrides(ruleSO, event._source) + ? // @ts-expect-error @elastic/elasticsearch _source is optional + buildRuleWithOverrides(ruleSO, event._source) : buildRuleWithoutOverrides(ruleSO); const signal: Signal = { ...buildSignal([event], rule), diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_event_type_signal.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_event_type_signal.test.ts index 0ae81770e83c29..185c1654429210 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_event_type_signal.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_event_type_signal.test.ts @@ -16,6 +16,7 @@ describe('buildEventTypeSignal', () => { test('it returns the event appended of kind signal if it does not exist', () => { const doc = sampleDocNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional delete doc._source.event; const eventType = buildEventTypeSignal(doc); const expected: object = { kind: 'signal' }; @@ -24,6 +25,7 @@ describe('buildEventTypeSignal', () => { test('it returns the event appended of kind signal if it is an empty object', () => { const doc = sampleDocNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional doc._source.event = {}; const eventType = buildEventTypeSignal(doc); const expected: object = { kind: 'signal' }; @@ -32,6 +34,7 @@ describe('buildEventTypeSignal', () => { test('it returns the event with kind signal and other properties if they exist', () => { const doc = sampleDocNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional doc._source.event = { action: 'socket_opened', module: 'system', diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_event_type_signal.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_event_type_signal.ts index bc267ba59b9c3e..374788f31a359c 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_event_type_signal.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_event_type_signal.ts @@ -4,12 +4,11 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - import { BaseSignalHit } from './types'; export const buildEventTypeSignal = (doc: BaseSignalHit): object => { - if (doc._source.event != null && doc._source.event instanceof Object) { - return { ...doc._source.event, kind: 'signal' }; + if (doc._source?.event != null && doc._source?.event instanceof Object) { + return { ...doc._source!.event, kind: 'signal' }; } else { return { kind: 'signal' }; } @@ -25,5 +24,5 @@ export const buildEventTypeSignal = (doc: BaseSignalHit): object => { * @param doc The document which might be a signal or it might be a regular log */ export const isEventTypeSignal = (doc: BaseSignalHit): boolean => { - return doc._source.signal?.rule?.id != null && typeof doc._source.signal?.rule?.id === 'string'; + return doc._source?.signal?.rule?.id != null && typeof doc._source?.signal?.rule?.id === 'string'; }; diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_events_query.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_events_query.ts index bce9adc9f0f882..e086c862262c16 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_events_query.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_events_query.ts @@ -4,18 +4,18 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import { SortOrderOrUndefined, TimestampOverrideOrUndefined, } from '../../../../common/detection_engine/schemas/common/schemas'; interface BuildEventsSearchQuery { - aggregations?: unknown; + aggregations?: Record; index: string[]; from: string; to: string; - filter: unknown; + filter?: estypes.QueryContainer; size: number; sortOrder?: SortOrderOrUndefined; searchAfterSortId: string | number | undefined; @@ -48,7 +48,7 @@ export const buildEventsSearchQuery = ({ ? timestampOverride : '@timestamp'; - const rangeFilter: unknown[] = [ + const rangeFilter: estypes.QueryContainer[] = [ { range: { [sortField]: { @@ -70,7 +70,9 @@ export const buildEventsSearchQuery = ({ }, }); } - const filterWithTime = [filter, { bool: { filter: rangeFilter } }]; + // @ts-expect-error undefined in not assignable to QueryContainer + // but tests contain undefined, so I suppose it's desired behaviour + const filterWithTime: estypes.QueryContainer[] = [filter, { bool: { filter: rangeFilter } }]; const searchQuery = { allow_no_indices: true, diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_rule.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_rule.test.ts index 48e04df3704ab1..757e6728f244ed 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_rule.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_rule.test.ts @@ -417,6 +417,7 @@ describe('buildRuleWithOverrides', () => { query: 'host.name: Braden', }, ]; + // @ts-expect-error @elastic/elasticsearch _source is optional const rule = buildRuleWithOverrides(ruleSO, sampleDocNoSortId()._source); const expected: RulesSchema = { ...expectedRule(), @@ -433,6 +434,7 @@ describe('buildRuleWithOverrides', () => { `${INTERNAL_RULE_ID_KEY}:rule-1`, `${INTERNAL_IMMUTABLE_KEY}:true`, ]; + // @ts-expect-error @elastic/elasticsearch _source is optional const rule = buildRuleWithOverrides(ruleSO, sampleDocNoSortId()._source); expect(rule).toEqual(expectedRule()); }); @@ -440,6 +442,7 @@ describe('buildRuleWithOverrides', () => { test('it applies rule name override in buildRule', () => { const ruleSO = sampleRuleSO(); ruleSO.attributes.params.ruleNameOverride = 'someKey'; + // @ts-expect-error @elastic/elasticsearch _source is optional const rule = buildRuleWithOverrides(ruleSO, sampleDocNoSortId()._source); const expected = { ...expectedRule(), @@ -465,7 +468,9 @@ describe('buildRuleWithOverrides', () => { }, ]; const doc = sampleDocNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional doc._source.new_risk_score = newRiskScore; + // @ts-expect-error @elastic/elasticsearch _source is optional const rule = buildRuleWithOverrides(ruleSO, doc._source); const expected = { ...expectedRule(), @@ -490,6 +495,7 @@ describe('buildRuleWithOverrides', () => { }, ]; const doc = sampleDocSeverity(Number(eventSeverity)); + // @ts-expect-error @elastic/elasticsearch _source is optional const rule = buildRuleWithOverrides(ruleSO, doc._source); const expected = { ...expectedRule(), diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_rule.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_rule.ts index 0681a5dddb127a..7755f2af70d844 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_rule.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_rule.ts @@ -47,18 +47,21 @@ export const buildRule = ({ throttle, }: BuildRuleParams): RulesSchema => { const { riskScore, riskScoreMeta } = buildRiskScoreFromMapping({ + // @ts-expect-error @elastic/elasticsearch _source is optional eventSource: doc._source, riskScore: ruleParams.riskScore, riskScoreMapping: ruleParams.riskScoreMapping, }); const { severity, severityMeta } = buildSeverityFromMapping({ + // @ts-expect-error @elastic/elasticsearch _source is optional eventSource: doc._source, severity: ruleParams.severity, severityMapping: ruleParams.severityMapping, }); const { ruleName, ruleNameMeta } = buildRuleNameFromMapping({ + // @ts-expect-error @elastic/elasticsearch _source is optional eventSource: doc._source, ruleName: name, ruleNameMapping: ruleParams.ruleNameOverride, diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_signal.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_signal.test.ts index 98b0d41bb0bf8a..6408b5fe9de103 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_signal.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_signal.test.ts @@ -28,6 +28,7 @@ describe('buildSignal', () => { test('it builds a signal as expected without original_event if event does not exist', () => { const doc = sampleDocNoSortId('d5e8eb51-a6a0-456d-8a15-4b79bfec3d71'); + // @ts-expect-error @elastic/elasticsearch _source is optional delete doc._source.event; const rule = getRulesSchemaMock(); const signal = { @@ -104,6 +105,7 @@ describe('buildSignal', () => { test('it builds a signal as expected with original_event if is present', () => { const doc = sampleDocNoSortId('d5e8eb51-a6a0-456d-8a15-4b79bfec3d71'); + // @ts-expect-error @elastic/elasticsearch _source is optional doc._source.event = { action: 'socket_opened', dataset: 'socket', @@ -191,6 +193,7 @@ describe('buildSignal', () => { test('it builds a ancestor correctly if the parent does not exist', () => { const doc = sampleDocNoSortId('d5e8eb51-a6a0-456d-8a15-4b79bfec3d71'); + // @ts-expect-error @elastic/elasticsearch _source is optional doc._source.event = { action: 'socket_opened', dataset: 'socket', @@ -209,12 +212,14 @@ describe('buildSignal', () => { test('it builds a ancestor correctly if the parent does exist', () => { const doc = sampleDocNoSortId('d5e8eb51-a6a0-456d-8a15-4b79bfec3d71'); + // @ts-expect-error @elastic/elasticsearch _source is optional doc._source.event = { action: 'socket_opened', dataset: 'socket', kind: 'event', module: 'system', }; + // @ts-expect-error @elastic/elasticsearch _source is optional doc._source.signal = { parents: [ { @@ -250,6 +255,7 @@ describe('buildSignal', () => { test('it builds a signal ancestor correctly if the parent does not exist', () => { const doc = sampleDocNoSortId('d5e8eb51-a6a0-456d-8a15-4b79bfec3d71'); + // @ts-expect-error @elastic/elasticsearch _source is optional doc._source.event = { action: 'socket_opened', dataset: 'socket', @@ -270,12 +276,14 @@ describe('buildSignal', () => { test('it builds a signal ancestor correctly if the parent does exist', () => { const doc = sampleDocNoSortId('d5e8eb51-a6a0-456d-8a15-4b79bfec3d71'); + // @ts-expect-error @elastic/elasticsearch _source is optional doc._source.event = { action: 'socket_opened', dataset: 'socket', kind: 'event', module: 'system', }; + // @ts-expect-error @elastic/elasticsearch _source is optional doc._source.signal = { parents: [ { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_signal.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_signal.ts index 78ff0e8e1e5dd8..237536a99c0f0e 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_signal.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/build_signal.ts @@ -17,15 +17,15 @@ import { Signal, Ancestor, BaseSignalHit, ThresholdResult } from './types'; * @param doc The parent signal or event */ export const buildParent = (doc: BaseSignalHit): Ancestor => { - if (doc._source.signal != null) { + if (doc._source?.signal != null) { return { - rule: doc._source.signal.rule.id, + rule: doc._source?.signal.rule.id, id: doc._id, type: 'signal', index: doc._index, // We first look for signal.depth and use that if it exists. If it doesn't exist, this should be a pre-7.10 signal // and should have signal.parent.depth instead. signal.parent.depth in this case is treated as equivalent to signal.depth. - depth: doc._source.signal.depth ?? doc._source.signal.parent?.depth ?? 1, + depth: doc._source?.signal.depth ?? doc._source?.signal.parent?.depth ?? 1, }; } else { return { @@ -44,7 +44,7 @@ export const buildParent = (doc: BaseSignalHit): Ancestor => { */ export const buildAncestors = (doc: BaseSignalHit): Ancestor[] => { const newAncestor = buildParent(doc); - const existingAncestors = doc._source.signal?.ancestors; + const existingAncestors = doc._source?.signal?.ancestors; if (existingAncestors != null) { return [...existingAncestors, newAncestor]; } else { @@ -59,6 +59,7 @@ export const buildAncestors = (doc: BaseSignalHit): Ancestor[] => { * @param doc The source index doc to a signal. */ export const removeClashes = (doc: BaseSignalHit): BaseSignalHit => { + // @ts-expect-error @elastic/elasticsearch _source is optional const { signal, ...noSignal } = doc._source; if (signal == null || isEventTypeSignal(doc)) { return doc; @@ -105,16 +106,17 @@ const isThresholdResult = (thresholdResult: SearchTypes): thresholdResult is Thr * @param doc The parent signal/event of the new signal to be built. */ export const additionalSignalFields = (doc: BaseSignalHit) => { - const thresholdResult = doc._source.threshold_result; + const thresholdResult = doc._source?.threshold_result; if (thresholdResult != null && !isThresholdResult(thresholdResult)) { throw new Error(`threshold_result failed to validate: ${thresholdResult}`); } return { parent: buildParent(removeClashes(doc)), + // @ts-expect-error @elastic/elasticsearch _source is optional original_time: doc._source['@timestamp'], // This field has already been replaced with timestampOverride, if provided. - original_event: doc._source.event ?? undefined, + original_event: doc._source?.event ?? undefined, threshold_result: thresholdResult, original_signal: - doc._source.signal != null && !isEventTypeSignal(doc) ? doc._source.signal : undefined, + doc._source?.signal != null && !isEventTypeSignal(doc) ? doc._source?.signal : undefined, }; }; diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/bulk_create_ml_signals.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/bulk_create_ml_signals.ts index c6a8c211417b8c..a5e05d07ee1e17 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/bulk_create_ml_signals.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/bulk_create_ml_signals.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import { flow, omit } from 'lodash/fp'; import set from 'set-value'; @@ -19,7 +19,6 @@ import { RuleTypeParams, RefreshTypes } from '../types'; import { singleBulkCreate, SingleBulkCreateResponse } from './single_bulk_create'; import { AnomalyResults, Anomaly } from '../../machine_learning'; import { BuildRuleMessage } from './rule_messages'; -import { SearchResponse } from '../../types'; interface BulkCreateMlSignalsParams { actions: RuleAlertAction[]; @@ -72,12 +71,18 @@ export const transformAnomalyFieldsToEcs = (anomaly: Anomaly): EcsAnomaly => { return flow(omitDottedFields, setNestedFields, setTimestamp)(anomaly); }; -const transformAnomalyResultsToEcs = (results: AnomalyResults): SearchResponse => { +const transformAnomalyResultsToEcs = ( + results: AnomalyResults +): estypes.SearchResponse => { const transformedHits = results.hits.hits.map(({ _source, ...rest }) => ({ ...rest, - _source: transformAnomalyFieldsToEcs(_source), + _source: transformAnomalyFieldsToEcs( + // @ts-expect-error @elastic/elasticsearch _source is optional + _source + ), })); + // @ts-expect-error Anomaly is not assignable to EcsAnomaly return { ...results, hits: { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/filters/filter_events.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/filters/filter_events.ts index 421ed91278f4cb..a50d40e33a7171 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/filters/filter_events.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/filters/filter_events.ts @@ -4,8 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - -import { SearchResponse } from '../../../types'; +import type { estypes } from '@elastic/elasticsearch'; import { FilterEventsOptions } from './types'; /** @@ -17,7 +16,7 @@ import { FilterEventsOptions } from './types'; export const filterEvents = ({ events, fieldAndSetTuples, -}: FilterEventsOptions): SearchResponse['hits']['hits'] => { +}: FilterEventsOptions): Array> => { return events.filter((item) => { return fieldAndSetTuples .map((tuple) => { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/filters/filter_events_against_list.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/filters/filter_events_against_list.ts index bb8fc5afb1ddfc..1320122626bfde 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/filters/filter_events_against_list.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/filters/filter_events_against_list.ts @@ -4,13 +4,12 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import { ExceptionListItemSchema, entriesList } from '../../../../../../lists/common/schemas'; import { hasLargeValueList } from '../../../../../common/detection_engine/utils'; import { FilterEventsAgainstListOptions } from './types'; import { filterEvents } from './filter_events'; import { createFieldAndSetTuples } from './create_field_and_set_tuples'; -import { SearchResponse } from '../../../types'; /** * Filters events against a large value based list. It does this through these @@ -39,7 +38,7 @@ export const filterEventsAgainstList = async ({ logger, eventSearchResult, buildRuleMessage, -}: FilterEventsAgainstListOptions): Promise> => { +}: FilterEventsAgainstListOptions): Promise> => { try { const atLeastOneLargeValueList = exceptionsList.some(({ entries }) => hasLargeValueList(entries) @@ -56,9 +55,9 @@ export const filterEventsAgainstList = async ({ return listItem.entries.every((entry) => entriesList.is(entry)); }); - const res = await valueListExceptionItems.reduce['hits']['hits']>>( + const res = await valueListExceptionItems.reduce>>>( async ( - filteredAccum: Promise['hits']['hits']>, + filteredAccum: Promise>>, exceptionItem: ExceptionListItemSchema ) => { const events = await filteredAccum; @@ -76,7 +75,7 @@ export const filterEventsAgainstList = async ({ ); return filteredEvents; }, - Promise.resolve['hits']['hits']>(eventSearchResult.hits.hits) + Promise.resolve>>(eventSearchResult.hits.hits) ); return { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/filters/types.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/filters/types.ts index d3bae848ab2a74..e1618d217d0dc0 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/filters/types.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/filters/types.ts @@ -4,24 +4,23 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import { Logger } from 'src/core/server'; import { ListClient } from '../../../../../../lists/server'; import { BuildRuleMessage } from '../rule_messages'; import { ExceptionListItemSchema, Type } from '../../../../../../lists/common/schemas'; -import { SearchResponse } from '../../../types'; export interface FilterEventsAgainstListOptions { listClient: ListClient; exceptionsList: ExceptionListItemSchema[]; logger: Logger; - eventSearchResult: SearchResponse; + eventSearchResult: estypes.SearchResponse; buildRuleMessage: BuildRuleMessage; } export interface CreateSetToFilterAgainstOptions { - events: SearchResponse['hits']['hits']; + events: Array>; field: string; listId: string; listType: Type; @@ -31,12 +30,12 @@ export interface CreateSetToFilterAgainstOptions { } export interface FilterEventsOptions { - events: SearchResponse['hits']['hits']; + events: Array>; fieldAndSetTuples: FieldSet[]; } export interface CreateFieldAndSetTuplesOptions { - events: SearchResponse['hits']['hits']; + events: Array>; exceptionItem: ExceptionListItemSchema; listClient: ListClient; logger: Logger; diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/mappings/build_risk_score_from_mapping.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/mappings/build_risk_score_from_mapping.test.ts index c21b4df1ac2c7f..88ce9de15cff80 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/mappings/build_risk_score_from_mapping.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/mappings/build_risk_score_from_mapping.test.ts @@ -195,6 +195,7 @@ interface TestCase { function testIt({ fieldValue, scoreDefault, scoreMapping, expected }: TestCase) { const result = buildRiskScoreFromMapping({ + // @ts-expect-error @elastic/elasticsearch _source is optional eventSource: sampleDocRiskScore(fieldValue)._source, riskScore: scoreDefault, riskScoreMapping: scoreMapping, diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/mappings/build_rule_name_from_mapping.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/mappings/build_rule_name_from_mapping.test.ts index 23e5aecc5c5536..b6281b637d4340 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/mappings/build_rule_name_from_mapping.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/mappings/build_rule_name_from_mapping.test.ts @@ -15,6 +15,7 @@ describe('buildRuleNameFromMapping', () => { test('rule name defaults to provided if mapping is incomplete', () => { const ruleName = buildRuleNameFromMapping({ + // @ts-expect-error @elastic/elasticsearch _source is optional eventSource: sampleDocNoSortId()._source, ruleName: 'rule-name', ruleNameMapping: 'message', diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/mappings/build_severity_from_mapping.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/mappings/build_severity_from_mapping.test.ts index 1264592331df0d..cfd4b81ae3bc8d 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/mappings/build_severity_from_mapping.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/mappings/build_severity_from_mapping.test.ts @@ -148,6 +148,7 @@ interface TestCase { function testIt({ fieldName, fieldValue, severityDefault, severityMapping, expected }: TestCase) { const result = buildSeverityFromMapping({ + // @ts-expect-error @elastic/elasticsearch _source is optional eventSource: sampleDocSeverity(fieldValue, fieldName)._source, severity: severityDefault, severityMapping, diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/search_after_bulk_create.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/search_after_bulk_create.test.ts index ccefa24e2018c2..6deb45095ec360 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/search_after_bulk_create.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/search_after_bulk_create.test.ts @@ -68,6 +68,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -88,6 +89,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -108,6 +110,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -128,6 +131,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -198,6 +202,7 @@ describe('searchAfterAndBulkCreate', () => { ) ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -218,6 +223,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -238,6 +244,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -309,6 +316,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -532,6 +540,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -617,6 +626,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -802,6 +812,7 @@ describe('searchAfterAndBulkCreate', () => { test('if returns false when singleSearchAfter throws an exception', async () => { mockService.scopedClusterClient.asCurrentUser.search .mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -906,6 +917,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -926,6 +938,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -946,6 +959,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -1010,6 +1024,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -1030,6 +1045,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -1050,6 +1066,7 @@ describe('searchAfterAndBulkCreate', () => { ); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/search_after_bulk_create.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/search_after_bulk_create.ts index 1dd3a2d2173a82..cfe30a66023813 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/search_after_bulk_create.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/search_after_bulk_create.ts @@ -95,6 +95,7 @@ export const searchAfterAndBulkCreate = async ({ to: tuple.to.toISOString(), services, logger, + // @ts-expect-error please, declare a type explicitly instead of unknown filter, pageSize: Math.ceil(Math.min(tuple.maxSignals, pageSize)), timestampOverride: ruleParams.timestampOverride, @@ -104,6 +105,7 @@ export const searchAfterAndBulkCreate = async ({ // call this function setSortIdOrExit() const lastSortId = searchResultB?.hits?.hits[searchResultB.hits.hits.length - 1]?.sort; if (lastSortId != null && lastSortId.length !== 0) { + // @ts-expect-error @elastic/elasticsearch SortResults contains null not assignable to backupSortId backupSortId = lastSortId[0]; hasBackupSortId = true; } else { @@ -135,6 +137,7 @@ export const searchAfterAndBulkCreate = async ({ to: tuple.to.toISOString(), services, logger, + // @ts-expect-error please, declare a type explicitly instead of unknown filter, pageSize: Math.ceil(Math.min(tuple.maxSignals, pageSize)), timestampOverride: ruleParams.timestampOverride, @@ -155,6 +158,7 @@ export const searchAfterAndBulkCreate = async ({ const lastSortId = searchResult.hits.hits[searchResult.hits.hits.length - 1]?.sort; if (lastSortId != null && lastSortId.length !== 0) { + // @ts-expect-error @elastic/elasticsearch SortResults contains null not assignable to sortId sortId = lastSortId[0]; hasSortId = true; } else { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/send_telemetry_events.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/send_telemetry_events.ts index 22dc8847087ed4..f7d21adc4bea97 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/send_telemetry_events.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/send_telemetry_events.ts @@ -16,7 +16,8 @@ export interface SearchResultWithSource { } export function selectEvents(filteredEvents: SignalSearchResponse): TelemetryEvent[] { - const sources = filteredEvents.hits.hits.map(function ( + // @ts-expect-error @elastic/elasticsearch _source is optional + const sources: TelemetryEvent[] = filteredEvents.hits.hits.map(function ( obj: SearchResultWithSource ): TelemetryEvent { return obj._source; diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/signal_rule_alert_type.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/signal_rule_alert_type.test.ts index bcd04ed5e15cd6..930cafe57fb0a3 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/signal_rule_alert_type.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/signal_rule_alert_type.test.ts @@ -6,6 +6,7 @@ */ import moment from 'moment'; +import type { estypes } from '@elastic/elasticsearch'; import { loggingSystemMock } from 'src/core/server/mocks'; import { getResult, @@ -149,12 +150,13 @@ describe('rules_notification_alert_type', () => { }, }) ); - const value: Partial = { + const value: Partial> = { statusCode: 200, body: { indices: ['index1', 'index2', 'index3', 'index4'], fields: { '@timestamp': { + // @ts-expect-error not full interface date: { indices: ['index1', 'index2', 'index3', 'index4'], searchable: true, @@ -165,7 +167,7 @@ describe('rules_notification_alert_type', () => { }, }; alertServices.scopedClusterClient.asCurrentUser.fieldCaps.mockResolvedValue( - value as ApiResponse + value as ApiResponse ); const ruleAlert = getResult(); alertServices.savedObjectsClient.get.mockResolvedValue({ diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_bulk_create.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_bulk_create.test.ts index eecedb02b26879..b9a771ac0299ea 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_bulk_create.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_bulk_create.test.ts @@ -142,6 +142,7 @@ describe('singleBulkCreate', () => { test('create successful bulk create', async () => { const sampleParams = sampleRuleAlertParams(); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not compatible response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -179,6 +180,7 @@ describe('singleBulkCreate', () => { test('create successful bulk create with docs with no versioning', async () => { const sampleParams = sampleRuleAlertParams(); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValueOnce( + // @ts-expect-error not compatible response interface elasticsearchClientMock.createSuccessTransportRequestPromise({ took: 100, errors: false, @@ -216,6 +218,7 @@ describe('singleBulkCreate', () => { test('create unsuccessful bulk create due to empty search results', async () => { const sampleParams = sampleRuleAlertParams(); mockService.scopedClusterClient.asCurrentUser.bulk.mockResolvedValue( + // @ts-expect-error not full response interface elasticsearchClientMock.createSuccessTransportRequestPromise(false) ); const { success, createdItemsCount } = await singleBulkCreate({ diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_bulk_create.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_bulk_create.ts index 6c791bc4d0ee3e..8a0788f6d42e6d 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_bulk_create.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_bulk_create.ts @@ -12,7 +12,7 @@ import { AlertInstanceState, AlertServices, } from '../../../../../alerting/server'; -import { SignalSearchResponse, BulkResponse, SignalHit, WrappedSignalHit } from './types'; +import { SignalHit, SignalSearchResponse, WrappedSignalHit } from './types'; import { RuleAlertAction } from '../../../../common/detection_engine/types'; import { RuleTypeParams, RefreshTypes } from '../types'; import { generateId, makeFloatString, errorAggregator } from './utils'; @@ -56,12 +56,12 @@ export const filterDuplicateRules = ( signalSearchResponse: SignalSearchResponse ) => { return signalSearchResponse.hits.hits.filter((doc) => { - if (doc._source.signal == null || !isEventTypeSignal(doc)) { + if (doc._source?.signal == null || !isEventTypeSignal(doc)) { return true; } else { return !( - doc._source.signal.ancestors.some((ancestor) => ancestor.rule === ruleId) || - doc._source.signal.rule.id === ruleId + doc._source?.signal.ancestors.some((ancestor) => ancestor.rule === ruleId) || + doc._source?.signal.rule.id === ruleId ); } }); @@ -158,7 +158,7 @@ export const singleBulkCreate = async ({ }), ]); const start = performance.now(); - const { body: response } = await services.scopedClusterClient.asCurrentUser.bulk({ + const { body: response } = await services.scopedClusterClient.asCurrentUser.bulk({ index: signalsIndex, refresh, body: bulkBody, @@ -244,7 +244,7 @@ export const bulkInsertSignals = async ( doc._source, ]); const start = performance.now(); - const { body: response } = await services.scopedClusterClient.asCurrentUser.bulk({ + const { body: response } = await services.scopedClusterClient.asCurrentUser.bulk({ refresh, body: bulkBody, }); diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_search_after.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_search_after.test.ts index a325903c66ec02..cbffac6e7b4553 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_search_after.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_search_after.test.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import { sampleDocSearchResultsNoSortId, mockLogger, @@ -12,7 +12,6 @@ import { } from './__mocks__/es_results'; import { singleSearchAfter } from './single_search_after'; import { alertsMock, AlertServicesMock } from '../../../../../alerting/server/mocks'; -import { ShardError } from '../../types'; import { buildRuleMessageFactory } from './rule_messages'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { elasticsearchClientMock } from 'src/core/server/elasticsearch/client/mocks'; @@ -69,7 +68,7 @@ describe('singleSearchAfter', () => { expect(searchErrors).toEqual([]); }); test('if singleSearchAfter will return an error array', async () => { - const errors: ShardError[] = [ + const errors: estypes.ShardFailure[] = [ { shard: 1, index: 'index-123', diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_search_after.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_search_after.ts index b35c68c8deacd5..9dcec1861f15dd 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_search_after.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/single_search_after.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import { performance } from 'perf_hooks'; import { AlertInstanceContext, @@ -12,7 +12,7 @@ import { AlertServices, } from '../../../../../alerting/server'; import { Logger } from '../../../../../../../src/core/server'; -import { SignalSearchResponse } from './types'; +import type { SignalSearchResponse, SignalSource } from './types'; import { BuildRuleMessage } from './rule_messages'; import { buildEventsSearchQuery } from './build_events_query'; import { createErrorsFromShard, makeFloatString } from './utils'; @@ -22,7 +22,7 @@ import { } from '../../../../common/detection_engine/schemas/common/schemas'; interface SingleSearchAfterParams { - aggregations?: unknown; + aggregations?: Record; searchAfterSortId: string | undefined; index: string[]; from: string; @@ -31,7 +31,7 @@ interface SingleSearchAfterParams { logger: Logger; pageSize: number; sortOrder?: SortOrderOrUndefined; - filter: unknown; + filter?: estypes.QueryContainer; timestampOverride: TimestampOverrideOrUndefined; buildRuleMessage: BuildRuleMessage; excludeDocsWithTimestampOverride: boolean; @@ -74,9 +74,7 @@ export const singleSearchAfter = async ({ const start = performance.now(); const { body: nextSearchAfterResult, - } = await services.scopedClusterClient.asCurrentUser.search( - searchAfterQuery - ); + } = await services.scopedClusterClient.asCurrentUser.search(searchAfterQuery); const end = performance.now(); const searchErrors = createErrorsFromShard({ errors: nextSearchAfterResult._shards.failures ?? [], diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/build_threat_mapping_filter.mock.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/build_threat_mapping_filter.mock.ts index 266903f568792c..e39b78b4f4a447 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/build_threat_mapping_filter.mock.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/build_threat_mapping_filter.mock.ts @@ -4,11 +4,10 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import { ThreatMapping } from '../../../../../common/detection_engine/schemas/types/threat_mapping'; import { Filter } from 'src/plugins/data/common'; -import { SearchResponse } from 'elasticsearch'; import { ThreatListDoc, ThreatListItem } from './types'; export const getThreatMappingMock = (): ThreatMapping => { @@ -62,7 +61,7 @@ export const getThreatMappingMock = (): ThreatMapping => { ]; }; -export const getThreatListSearchResponseMock = (): SearchResponse => ({ +export const getThreatListSearchResponseMock = (): estypes.SearchResponse => ({ took: 0, timed_out: false, _shards: { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/create_threat_signals.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/create_threat_signals.ts index e0be48458b0493..01465729413317 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/create_threat_signals.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/create_threat_signals.ts @@ -172,6 +172,7 @@ export const createThreatSignals = async ({ language: threatLanguage, threatFilters, index: threatIndex, + // @ts-expect-error@elastic/elasticsearch SortResults might contain null searchAfter: threatList.hits.hits[threatList.hits.hits.length - 1].sort, sortField: undefined, sortOrder: undefined, diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/enrich_signal_threat_matches.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/enrich_signal_threat_matches.ts index bc2be4ecaab329..83a3ce8cb773f7 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/enrich_signal_threat_matches.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/enrich_signal_threat_matches.ts @@ -101,7 +101,7 @@ export const enrichSignalThreatMatches = async ( return { ...signalHit, _source: { - ...signalHit._source, + ...signalHit._source!, threat: { ...threat, indicator: [...existingIndicators, ...matchedIndicators[i]], diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/get_threat_list.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/get_threat_list.ts index a2a51d3a060c17..c3d3d6c6a99e13 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/get_threat_list.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/get_threat_list.ts @@ -5,15 +5,14 @@ * 2.0. */ -import { ApiResponse } from '@elastic/elasticsearch'; -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { getQueryFilter } from '../../../../../common/detection_engine/get_query_filter'; import { GetSortWithTieBreakerOptions, GetThreatListOptions, SortWithTieBreaker, ThreatListCountOptions, - ThreatListItem, + ThreatListDoc, } from './types'; /** @@ -35,7 +34,7 @@ export const getThreatList = async ({ listClient, buildRuleMessage, logger, -}: GetThreatListOptions): Promise> => { +}: GetThreatListOptions): Promise> => { const calculatedPerPage = perPage ?? MAX_PER_PAGE; if (calculatedPerPage > 10000) { throw new TypeError('perPage cannot exceed the size of 10000'); @@ -53,8 +52,9 @@ export const getThreatList = async ({ `Querying the indicator items from the index: "${index}" with searchAfter: "${searchAfter}" for up to ${calculatedPerPage} indicator items` ) ); - const { body: response } = await esClient.search>({ + const { body: response } = await esClient.search({ body: { + // @ts-expect-error ESBoolQuery is not assignale to QueryContainer query: queryFilter, fields: [ { @@ -123,12 +123,9 @@ export const getThreatListCount = async ({ index, exceptionItems ); - const { - body: response, - }: ApiResponse<{ - count: number; - }> = await esClient.count({ + const { body: response } = await esClient.count({ body: { + // @ts-expect-error ESBoolQuery is not assignale to QueryContainer query: queryFilter, }, ignore_unavailable: true, diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/types.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/types.ts index 0c14f906742d4d..65b59d4df07911 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/types.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threat_mapping/types.ts @@ -4,9 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - -import { SearchResponse } from 'elasticsearch'; - +import type { estypes } from '@elastic/elasticsearch'; import { ListClient } from '../../../../../../lists/server'; import { Type, @@ -187,7 +185,7 @@ export interface ThreatListDoc { * This is an ECS document being returned, but the user could return or use non-ecs based * documents potentially. */ -export type ThreatListItem = SearchResponse['hits']['hits'][number]; +export type ThreatListItem = estypes.Hit; export interface ThreatIndicator { [key: string]: unknown; diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold/bulk_create_threshold_signals.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold/bulk_create_threshold_signals.ts index 43158d1e897832..8e5e31cc87b4f0 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold/bulk_create_threshold_signals.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold/bulk_create_threshold_signals.ts @@ -7,7 +7,6 @@ import { get } from 'lodash/fp'; import set from 'set-value'; - import { normalizeThresholdField } from '../../../../../common/detection_engine/utils'; import { ThresholdNormalized, @@ -29,10 +28,10 @@ import { getThresholdTermsHash, } from '../utils'; import { BuildRuleMessage } from '../rule_messages'; -import { +import type { MultiAggBucket, - SignalSearchResponse, SignalSource, + SignalSearchResponse, ThresholdSignalHistory, } from '../types'; @@ -141,7 +140,8 @@ const getTransformedHits = ( // Recurse through the nested buckets and collect each unique combination of terms. Collect the // cardinality and document count from the leaf buckets and return a signal for each set of terms. - return getCombinations(results.aggregations[aggParts.name].buckets, 0, aggParts.field).reduce( + // @ts-expect-error @elastic/elasticsearch no way to declare a type for aggregation in the search response + return getCombinations(results.aggregations![aggParts.name].buckets, 0, aggParts.field).reduce( (acc: Array>, bucket) => { const hit = bucket.topThresholdHits?.hits.hits[0]; if (hit == null) { diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold/find_threshold_signals.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold/find_threshold_signals.ts index 7dda263dd9f0a6..efcdb85e9b2c73 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold/find_threshold_signals.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold/find_threshold_signals.ts @@ -20,7 +20,7 @@ import { import { Logger } from '../../../../../../../../src/core/server'; import { BuildRuleMessage } from '../rule_messages'; import { singleSearchAfter } from '../single_search_after'; -import { SignalSearchResponse } from '../types'; +import type { SignalSearchResponse } from '../types'; interface FindThresholdSignalsParams { from: string; @@ -56,7 +56,7 @@ export const findThresholdSignals = async ({ sort: [ { [timestampOverride ?? '@timestamp']: { - order: 'desc', + order: 'desc' as const, }, }, ], @@ -137,6 +137,7 @@ export const findThresholdSignals = async ({ to, services, logger, + // @ts-expect-error refactor to pass type explicitly instead of unknown filter, pageSize: 1, sortOrder: 'desc', diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold/get_threshold_bucket_filters.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold/get_threshold_bucket_filters.ts index 73068a73a38a3b..4dd21938690dba 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold/get_threshold_bucket_filters.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/threshold/get_threshold_bucket_filters.ts @@ -34,7 +34,7 @@ export const getThresholdBucketFilters = async ({ bucket.terms.forEach((term) => { if (term.field != null) { - (filter.bool.filter as ESFilter[]).push({ + (filter.bool!.filter as ESFilter[]).push({ term: { [term.field]: `${term.value}`, }, diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/types.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/types.ts index 013d4a07cbeb79..559c5875c90d00 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/types.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/types.ts @@ -5,6 +5,7 @@ * 2.0. */ +import type { estypes } from '@elastic/elasticsearch'; import { DslQuery, Filter } from 'src/plugins/data/common'; import moment, { Moment } from 'moment'; import { Status } from '../../../../common/detection_engine/schemas/common/schemas'; @@ -17,7 +18,7 @@ import { AlertExecutorOptions, AlertServices, } from '../../../../../alerting/server'; -import { BaseSearchResponse, SearchHit, SearchResponse, TermAggregationBucket } from '../../types'; +import { BaseSearchResponse, SearchHit, TermAggregationBucket } from '../../types'; import { EqlSearchResponse, BaseHit, @@ -150,11 +151,10 @@ export interface GetResponse { _source: SearchTypes; } -export type EventSearchResponse = SearchResponse; -export type SignalSearchResponse = SearchResponse; -export type SignalSourceHit = SignalSearchResponse['hits']['hits'][number]; +export type SignalSearchResponse = estypes.SearchResponse; +export type SignalSourceHit = estypes.Hit; export type WrappedSignalHit = BaseHit; -export type BaseSignalHit = BaseHit; +export type BaseSignalHit = estypes.Hit; export type EqlSignalSearchResponse = EqlSearchResponse; diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/utils.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/utils.test.ts index 6d7948baa9681d..249305ebcd9a1d 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/utils.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/utils.test.ts @@ -1133,6 +1133,7 @@ describe('utils', () => { test('result with error will create success: false within the result set', () => { const searchResult = sampleDocSearchResultsNoSortIdNoHits(); searchResult._shards.failed = 1; + // @ts-expect-error not full interface searchResult._shards.failures = [{ reason: { reason: 'Not a sort failure' } }]; const { success } = createSearchAfterReturnTypeFromResponse({ searchResult, @@ -1144,6 +1145,7 @@ describe('utils', () => { test('result with error will create success: false within the result set if failed is 2 or more', () => { const searchResult = sampleDocSearchResultsNoSortIdNoHits(); searchResult._shards.failed = 2; + // @ts-expect-error not full interface searchResult._shards.failures = [{ reason: { reason: 'Not a sort failure' } }]; const { success } = createSearchAfterReturnTypeFromResponse({ searchResult, @@ -1156,7 +1158,9 @@ describe('utils', () => { const searchResult = sampleDocSearchResultsNoSortIdNoHits(); searchResult._shards.failed = 2; searchResult._shards.failures = [ + // @ts-expect-error not full interface { reason: { reason: 'Not a sort failure' } }, + // @ts-expect-error not full interface { reason: { reason: 'No mapping found for [@timestamp] in order to sort on' } }, ]; const { success } = createSearchAfterReturnTypeFromResponse({ @@ -1180,7 +1184,9 @@ describe('utils', () => { const searchResult = sampleDocSearchResultsNoSortIdNoHits(); searchResult._shards.failed = 2; searchResult._shards.failures = [ + // @ts-expect-error not full interface { reason: { reason: 'No mapping found for [event.ingested] in order to sort on' } }, + // @ts-expect-error not full interface { reason: { reason: 'No mapping found for [@timestamp] in order to sort on' } }, ]; const { success } = createSearchAfterReturnTypeFromResponse({ @@ -1192,6 +1198,7 @@ describe('utils', () => { test('It will not set an invalid date time stamp from a non-existent @timestamp when the index is not 100% ECS compliant', () => { const searchResult = sampleDocSearchResultsNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional (searchResult.hits.hits[0]._source['@timestamp'] as unknown) = undefined; if (searchResult.hits.hits[0].fields != null) { (searchResult.hits.hits[0].fields['@timestamp'] as unknown) = undefined; @@ -1205,6 +1212,7 @@ describe('utils', () => { test('It will not set an invalid date time stamp from a null @timestamp when the index is not 100% ECS compliant', () => { const searchResult = sampleDocSearchResultsNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional (searchResult.hits.hits[0]._source['@timestamp'] as unknown) = null; if (searchResult.hits.hits[0].fields != null) { (searchResult.hits.hits[0].fields['@timestamp'] as unknown) = null; @@ -1218,6 +1226,7 @@ describe('utils', () => { test('It will not set an invalid date time stamp from an invalid @timestamp string', () => { const searchResult = sampleDocSearchResultsNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional (searchResult.hits.hits[0]._source['@timestamp'] as unknown) = 'invalid'; if (searchResult.hits.hits[0].fields != null) { (searchResult.hits.hits[0].fields['@timestamp'] as unknown) = ['invalid']; @@ -1233,6 +1242,7 @@ describe('utils', () => { describe('lastValidDate', () => { test('It returns undefined if the search result contains a null timestamp', () => { const searchResult = sampleDocSearchResultsNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional (searchResult.hits.hits[0]._source['@timestamp'] as unknown) = null; if (searchResult.hits.hits[0].fields != null) { (searchResult.hits.hits[0].fields['@timestamp'] as unknown) = null; @@ -1243,6 +1253,7 @@ describe('utils', () => { test('It returns undefined if the search result contains a undefined timestamp', () => { const searchResult = sampleDocSearchResultsNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional (searchResult.hits.hits[0]._source['@timestamp'] as unknown) = undefined; if (searchResult.hits.hits[0].fields != null) { (searchResult.hits.hits[0].fields['@timestamp'] as unknown) = undefined; @@ -1253,6 +1264,7 @@ describe('utils', () => { test('It returns undefined if the search result contains an invalid string value', () => { const searchResult = sampleDocSearchResultsNoSortId(); + // @ts-expect-error @elastic/elasticsearch _source is optional (searchResult.hits.hits[0]._source['@timestamp'] as unknown) = 'invalid value'; if (searchResult.hits.hits[0].fields != null) { (searchResult.hits.hits[0].fields['@timestamp'] as unknown) = ['invalid value']; @@ -1286,7 +1298,7 @@ describe('utils', () => { test('It returns timestampOverride date time if set', () => { const override = '2020-10-07T19:20:28.049Z'; const searchResult = sampleDocSearchResultsNoSortId(); - searchResult.hits.hits[0]._source.different_timestamp = new Date(override).toISOString(); + searchResult.hits.hits[0]._source!.different_timestamp = new Date(override).toISOString(); const date = lastValidDate({ searchResult, timestampOverride: 'different_timestamp' }); expect(date?.toISOString()).toEqual(override); }); diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/utils.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/utils.ts index fa2fa1f102bd14..28edd97de0a0e9 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/signals/utils.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/signals/utils.ts @@ -9,6 +9,7 @@ import { createHash } from 'crypto'; import moment from 'moment'; import uuidv5 from 'uuid/v5'; import dateMath from '@elastic/datemath'; +import type { estypes } from '@elastic/elasticsearch'; import { isEmpty, partition } from 'lodash'; import { ApiResponse, Context } from '@elastic/elasticsearch/lib/Transport'; @@ -27,7 +28,6 @@ import { ExceptionListClient, ListClient, ListPluginSetup } from '../../../../.. import { ExceptionListItemSchema } from '../../../../../lists/common/schemas'; import { ListArray } from '../../../../common/detection_engine/schemas/types/lists'; import { - BulkResponse, BulkResponseErrorAggregation, SignalHit, SearchAfterAndBulkCreateReturnType, @@ -408,7 +408,7 @@ export const makeFloatString = (num: number): string => Number(num).toFixed(2); * @returns The aggregated example as shown above. */ export const errorAggregator = ( - response: BulkResponse, + response: estypes.BulkResponse, ignoreStatusCodes: number[] ): BulkResponseErrorAggregation => { return response.items.reduce((accum, item) => { @@ -568,7 +568,7 @@ export const lastValidDate = ({ searchResult, timestampOverride, }: { - searchResult: SignalSearchResponse; + searchResult: estypes.SearchResponse; timestampOverride: TimestampOverrideOrUndefined; }): Date | undefined => { if (searchResult.hits.hits.length === 0) { @@ -579,7 +579,8 @@ export const lastValidDate = ({ const timestampValue = lastRecord.fields != null && lastRecord.fields[timestamp] != null ? lastRecord.fields[timestamp][0] - : lastRecord._source[timestamp]; + : // @ts-expect-error @elastic/elasticsearch _source is optional + lastRecord._source[timestamp]; const lastTimestamp = typeof timestampValue === 'string' || typeof timestampValue === 'number' ? timestampValue @@ -599,7 +600,7 @@ export const createSearchAfterReturnTypeFromResponse = ({ searchResult, timestampOverride, }: { - searchResult: SignalSearchResponse; + searchResult: estypes.SearchResponse; timestampOverride: TimestampOverrideOrUndefined; }): SearchAfterAndBulkCreateReturnType => { return createSearchAfterReturnType({ @@ -730,6 +731,7 @@ export const mergeSearchResults = (searchResults: SignalSearchResponse[]) => { total: newShards.total + existingShards.total, successful: newShards.successful + existingShards.successful, failed: newShards.failed + existingShards.failed, + // @ts-expect-error @elastic/elaticsearch skipped is optional in ShardStatistics skipped: newShards.skipped + existingShards.skipped, failures: [ ...(existingShards.failures != null ? existingShards.failures : []), @@ -741,7 +743,7 @@ export const mergeSearchResults = (searchResults: SignalSearchResponse[]) => { total: createTotalHitsFromSearchResult({ searchResult: prev }) + createTotalHitsFromSearchResult({ searchResult: next }), - max_score: Math.max(newHits.max_score, existingHits.max_score), + max_score: Math.max(newHits.max_score!, existingHits.max_score!), hits: [...existingHits.hits, ...newHits.hits], }, }; @@ -751,7 +753,7 @@ export const mergeSearchResults = (searchResults: SignalSearchResponse[]) => { export const createTotalHitsFromSearchResult = ({ searchResult, }: { - searchResult: SignalSearchResponse; + searchResult: { hits: { total: number | { value: number } } }; }): number => { const totalHits = typeof searchResult.hits.total === 'number' diff --git a/x-pack/plugins/security_solution/server/lib/machine_learning/index.ts b/x-pack/plugins/security_solution/server/lib/machine_learning/index.ts index 6aac390a3f842b..db42dc2720b2a8 100644 --- a/x-pack/plugins/security_solution/server/lib/machine_learning/index.ts +++ b/x-pack/plugins/security_solution/server/lib/machine_learning/index.ts @@ -5,19 +5,18 @@ * 2.0. */ -import { RequestParams } from '@elastic/elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { buildExceptionFilter } from '../../../common/shared_imports'; import { ExceptionListItemSchema } from '../../../../lists/common'; import { AnomalyRecordDoc as Anomaly } from '../../../../ml/server'; -import { SearchResponse } from '../types'; export { Anomaly }; -export type AnomalyResults = SearchResponse; +export type AnomalyResults = estypes.SearchResponse; type MlAnomalySearch = ( - searchParams: RequestParams.Search, + searchParams: estypes.SearchRequest, jobIds: string[] -) => Promise>; +) => Promise>; export interface AnomaliesSearchParams { jobIds: string[]; @@ -67,7 +66,7 @@ export const getAnomalies = async ( include_unmapped: true, }, ], - sort: [{ record_score: { order: 'desc' } }], + sort: [{ record_score: { order: 'desc' as const } }], }, }, params.jobIds diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/all/query.all_hosts.dsl.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/all/query.all_hosts.dsl.ts index 892801a3aed0b6..5d8540f8860772 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/all/query.all_hosts.dsl.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/all/query.all_hosts.dsl.ts @@ -58,7 +58,7 @@ export const buildHostsQuery = ({ sort: [ { '@timestamp': { - order: 'desc', + order: 'desc' as const, }, }, ], diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/authentications/dsl/query.dsl.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/authentications/dsl/query.dsl.ts index d1925e84bc5e0a..e960067713bda2 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/authentications/dsl/query.dsl.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/authentications/dsl/query.dsl.ts @@ -6,7 +6,7 @@ */ import { isEmpty } from 'lodash/fp'; - +import type { estypes } from '@elastic/elasticsearch'; import { HostAuthenticationsRequestOptions } from '../../../../../../../common/search_strategy/security_solution/hosts/authentications'; import { sourceFieldsMap, hostFieldsMap } from '../../../../../../../common/ecs/ecs_fields'; @@ -33,7 +33,10 @@ export const buildQuery = ({ defaultIndex, docValueFields, }: HostAuthenticationsRequestOptions) => { - const esFields = reduceFields(authenticationsFields, { ...hostFieldsMap, ...sourceFieldsMap }); + const esFields = reduceFields(authenticationsFields, { + ...hostFieldsMap, + ...sourceFieldsMap, + }) as string[]; const filter = [ ...createQueryFilterClauses(filterQuery), @@ -69,7 +72,10 @@ export const buildQuery = ({ terms: { size: querySize, field: 'user.name', - order: [{ 'successes.doc_count': 'desc' }, { 'failures.doc_count': 'desc' }], + order: [ + { 'successes.doc_count': 'desc' as const }, + { 'failures.doc_count': 'desc' as const }, + ] as estypes.TermsAggregationOrder, }, aggs: { failures: { @@ -83,7 +89,7 @@ export const buildQuery = ({ top_hits: { size: 1, _source: esFields, - sort: [{ '@timestamp': { order: 'desc' } }], + sort: [{ '@timestamp': { order: 'desc' as const } }], }, }, }, @@ -99,7 +105,7 @@ export const buildQuery = ({ top_hits: { size: 1, _source: esFields, - sort: [{ '@timestamp': { order: 'desc' } }], + sort: [{ '@timestamp': { order: 'desc' as const } }], }, }, }, diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/kpi/authentications/query.hosts_kpi_authentications.dsl.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/kpi/authentications/query.hosts_kpi_authentications.dsl.ts index a5c82688e01ba2..01473a4368dbca 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/kpi/authentications/query.hosts_kpi_authentications.dsl.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/kpi/authentications/query.hosts_kpi_authentications.dsl.ts @@ -54,7 +54,7 @@ export const buildHostsKpiAuthenticationsQuery = ({ authentication_success_histogram: { auto_date_histogram: { field: '@timestamp', - buckets: '6', + buckets: 6, }, aggs: { count: { @@ -76,7 +76,7 @@ export const buildHostsKpiAuthenticationsQuery = ({ authentication_failure_histogram: { auto_date_histogram: { field: '@timestamp', - buckets: '6', + buckets: 6, }, aggs: { count: { diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/kpi/hosts/query.hosts_kpi_hosts.dsl.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/kpi/hosts/query.hosts_kpi_hosts.dsl.ts index 0e0cbd8a2649d0..5ea2d1aa407805 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/kpi/hosts/query.hosts_kpi_hosts.dsl.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/kpi/hosts/query.hosts_kpi_hosts.dsl.ts @@ -41,7 +41,7 @@ export const buildHostsKpiHostsQuery = ({ hosts_histogram: { auto_date_histogram: { field: '@timestamp', - buckets: '6', + buckets: 6, }, aggs: { count: { diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/kpi/unique_ips/query.hosts_kpi_unique_ips.dsl.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/kpi/unique_ips/query.hosts_kpi_unique_ips.dsl.ts index a702982ab8253d..0471644d11bbea 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/kpi/unique_ips/query.hosts_kpi_unique_ips.dsl.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/kpi/unique_ips/query.hosts_kpi_unique_ips.dsl.ts @@ -41,7 +41,7 @@ export const buildHostsKpiUniqueIpsQuery = ({ unique_source_ips_histogram: { auto_date_histogram: { field: '@timestamp', - buckets: '6', + buckets: 6, }, aggs: { count: { @@ -59,7 +59,7 @@ export const buildHostsKpiUniqueIpsQuery = ({ unique_destination_ips_histogram: { auto_date_histogram: { field: '@timestamp', - buckets: '6', + buckets: 6, }, aggs: { count: { diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/overview/index.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/overview/index.ts index e040a94fb35150..c58e450806849a 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/overview/index.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/overview/index.ts @@ -24,6 +24,7 @@ export const hostOverview: SecuritySolutionFactory = { options: HostOverviewRequestOptions, response: IEsSearchResponse ): Promise => { + // @ts-expect-error @elastic/elasticsearch no way to declare type for aggregations const aggregations: OverviewHostHit = get('aggregations', response.rawResponse) || {}; const inspect = { dsl: [inspectStringifyObject(buildOverviewHostQuery(options))], diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/overview/query.overview_host.dsl.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/overview/query.overview_host.dsl.ts index 2c237ab75bcbbe..897ae633076a21 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/overview/query.overview_host.dsl.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/overview/query.overview_host.dsl.ts @@ -291,7 +291,8 @@ export const buildOverviewHostQuery = ({ }, size: 0, }, - }; + } as const; + // @ts-expect-error @elastic-elasticsearch readonly [] is not assignable to mutable QueryContainer[] return dslQuery; }; diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/uncommon_processes/dsl/query.dsl.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/uncommon_processes/dsl/query.dsl.ts index f036d7c943663e..1df0d60d54a88a 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/uncommon_processes/dsl/query.dsl.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/hosts/uncommon_processes/dsl/query.dsl.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import { createQueryFilterClauses } from '../../../../../../utils/build_query'; import { reduceFields } from '../../../../../../utils/build_query/reduce_fields'; import { @@ -24,8 +24,8 @@ export const buildQuery = ({ const processUserFields = reduceFields(uncommonProcessesFields, { ...processFieldsMap, ...userFieldsMap, - }); - const hostFields = reduceFields(uncommonProcessesFields, hostFieldsMap); + }) as string[]; + const hostFields = reduceFields(uncommonProcessesFields, hostFieldsMap) as string[]; const filter = [ ...createQueryFilterClauses(filterQuery), { @@ -60,21 +60,21 @@ export const buildQuery = ({ field: 'process.name', order: [ { - host_count: 'asc', + host_count: 'asc' as const, }, { - _count: 'asc', + _count: 'asc' as const, }, { - _key: 'asc', + _key: 'asc' as const, }, - ], + ] as estypes.TermsAggregationOrder, }, aggregations: { process: { top_hits: { size: 1, - sort: [{ '@timestamp': { order: 'desc' } }], + sort: [{ '@timestamp': { order: 'desc' as const } }], _source: processUserFields, }, }, @@ -120,7 +120,7 @@ export const buildQuery = ({ 'event.action': 'executed', }, }, - ], + ] as estypes.QueryContainer[], }, }, { @@ -146,7 +146,7 @@ export const buildQuery = ({ 'event.action': 'process_started', }, }, - ], + ] as estypes.QueryContainer[], }, }, { diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/matrix_histogram/index.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/matrix_histogram/index.ts index dfb157e148c9ed..40b22a31691b63 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/matrix_histogram/index.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/matrix_histogram/index.ts @@ -63,6 +63,7 @@ export const matrixHistogram: SecuritySolutionFactory = { results: { hits: { total: { value: 1, relation: 'eq' }, - max_score: null, + max_score: undefined, hits: [ { _index: 'auditbeat-7.8.0-2020.11.23-000004', _id: 'wRCuOnYB7WTwW_GluxL8', - _score: null, + _score: undefined, _source: { host: { hostname: 'internal-ci-immutable-rm-ubuntu-2004-big2-1607296224012102773', @@ -188,12 +188,12 @@ export const mockSearchStrategyResponse: IEsSearchResponse = { results: { hits: { total: { value: 5, relation: 'eq' }, - max_score: null, + max_score: undefined, hits: [ { _index: 'filebeat-8.0.0-2020.09.02-000001', _id: 'dd4fa2d4bd-1523631609876537', - _score: null, + _score: undefined, _source: { destination: { geo: { @@ -217,12 +217,12 @@ export const mockSearchStrategyResponse: IEsSearchResponse = { results: { hits: { total: { value: 5, relation: 'eq' }, - max_score: null, + max_score: undefined, hits: [ { _index: 'filebeat-8.0.0-2020.09.02-000001', _id: 'dd4fa2d4bd-1523631609876537', - _score: null, + _score: undefined, _source: { destination: { as: { number: 15169, organization: { name: 'Google LLC' } } }, }, @@ -244,12 +244,12 @@ export const mockSearchStrategyResponse: IEsSearchResponse = { results: { hits: { total: { value: 5, relation: 'eq' }, - max_score: null, + max_score: undefined, hits: [ { _index: 'filebeat-8.0.0-2020.09.02-000001', _id: 'dd4fa2d4bd-1523631486500511', - _score: null, + _score: undefined, _source: { source: { geo: { @@ -273,12 +273,12 @@ export const mockSearchStrategyResponse: IEsSearchResponse = { results: { hits: { total: { value: 5, relation: 'eq' }, - max_score: null, + max_score: undefined, hits: [ { _index: 'filebeat-8.0.0-2020.09.02-000001', _id: 'dd4fa2d4bd-1523631486500511', - _score: null, + _score: undefined, _source: { source: { as: { number: 15169, organization: { name: 'Google LLC' } } }, }, diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/details/query.details_network.dsl.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/details/query.details_network.dsl.ts index d1d0c44d9b61b0..e5a508663a2e0b 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/details/query.details_network.dsl.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/details/query.details_network.dsl.ts @@ -40,7 +40,7 @@ const getAggs = (type: string, ip: string) => { _source: [`${type}.as`], sort: [ { - '@timestamp': 'desc', + '@timestamp': 'desc' as const, }, ], }, @@ -60,7 +60,7 @@ const getAggs = (type: string, ip: string) => { _source: [`${type}.geo`], sort: [ { - '@timestamp': 'desc', + '@timestamp': 'desc' as const, }, ], }, @@ -87,7 +87,7 @@ const getHostAggs = (ip: string) => { _source: ['host'], sort: [ { - '@timestamp': 'desc', + '@timestamp': 'desc' as const, }, ], }, diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/dns/index.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/dns/index.ts index 5ab2beaabd3cb7..9a73fb30a074d8 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/dns/index.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/dns/index.ts @@ -24,6 +24,7 @@ import { getDnsEdges } from './helpers'; import { buildDnsQuery } from './query.dns_network.dsl'; export const networkDns: SecuritySolutionFactory = { + // @ts-expect-error dns_name_query_count is incompatbile. Maybe' is not assignable to type 'string | undefined buildDsl: (options: NetworkDnsRequestOptions) => { if (options.pagination && options.pagination.querySize >= DEFAULT_MAX_TABLE_QUERY_SIZE) { throw new Error(`No query size above ${DEFAULT_MAX_TABLE_QUERY_SIZE}`); diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/http/index.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/http/index.ts index 259b45f436124c..6a36e113b62a7a 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/http/index.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/http/index.ts @@ -24,6 +24,7 @@ import { getHttpEdges } from './helpers'; import { buildHttpQuery } from './query.http_network.dsl'; export const networkHttp: SecuritySolutionFactory = { + // @ts-expect-error dns_name_query_count is not conpatible with @elastic/elasticsearch buildDsl: (options: NetworkHttpRequestOptions) => { if (options.pagination && options.pagination.querySize >= DEFAULT_MAX_TABLE_QUERY_SIZE) { throw new Error(`No query size above ${DEFAULT_MAX_TABLE_QUERY_SIZE}`); diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/kpi/dns/index.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/kpi/dns/index.ts index 9f14b16971b5f9..7ef0e6e3035281 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/kpi/dns/index.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/kpi/dns/index.ts @@ -28,6 +28,7 @@ export const networkKpiDns: SecuritySolutionFactory = { return { ...response, inspect, + // @ts-expect-error code doesn't handle TotalHits dnsQueries: response.rawResponse.hits.total, }; }, diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/kpi/network_events/index.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/kpi/network_events/index.ts index 2956110239e695..5327a2396cdace 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/kpi/network_events/index.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/kpi/network_events/index.ts @@ -28,6 +28,7 @@ export const networkKpiNetworkEvents: SecuritySolutionFactory = { + // @ts-expect-error auto_date_histogram.buckets is incompatible buildDsl: (options: NetworkKpiUniquePrivateIpsRequestOptions) => buildUniquePrivateIpsQuery(options), parse: async ( diff --git a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/overview/index.ts b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/overview/index.ts index 7a7650b0f77d7a..1f85a119f3c8e6 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/overview/index.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/security_solution/factory/network/overview/index.ts @@ -24,6 +24,7 @@ export const networkOverview: SecuritySolutionFactory = options: NetworkOverviewRequestOptions, response: IEsSearchResponse ): Promise => { + // @ts-expect-error @elastic/elasticsearch no way to declare type for aggregations const aggregations: OverviewNetworkHit = get('aggregations', response.rawResponse) || {}; const inspect = { dsl: [inspectStringifyObject(buildOverviewNetworkQuery(options))], diff --git a/x-pack/plugins/security_solution/server/search_strategy/timeline/factory/events/all/index.ts b/x-pack/plugins/security_solution/server/search_strategy/timeline/factory/events/all/index.ts index 05058e3ee7a2dc..172c864f7ee4f2 100644 --- a/x-pack/plugins/security_solution/server/search_strategy/timeline/factory/events/all/index.ts +++ b/x-pack/plugins/security_solution/server/search_strategy/timeline/factory/events/all/index.ts @@ -53,6 +53,7 @@ export const timelineEventsAll: SecuritySolutionTimelineFactory( getDataFromSourceHits, + // @ts-expect-error @elastic/elasticsearch _source is optional _source ); const fieldsData = await getDataSafety( diff --git a/x-pack/plugins/security_solution/server/usage/detections/detections_helpers.ts b/x-pack/plugins/security_solution/server/usage/detections/detections_helpers.ts index 4236c782d6c685..211c477027eec9 100644 --- a/x-pack/plugins/security_solution/server/usage/detections/detections_helpers.ts +++ b/x-pack/plugins/security_solution/server/usage/detections/detections_helpers.ts @@ -9,7 +9,6 @@ import { ElasticsearchClient, SavedObjectsClientContract, KibanaRequest, - SearchResponse, } from '../../../../../../src/core/server'; import { MlPluginSetup } from '../../../../ml/server'; import { SIGNALS_ID, INTERNAL_IMMUTABLE_KEY } from '../../../common/constants'; @@ -167,14 +166,13 @@ export const getRulesUsage = async ( }; try { - const { body: ruleResults } = await esClient.search>( - ruleSearchOptions - ); + const { body: ruleResults } = await esClient.search(ruleSearchOptions); if (ruleResults.hits?.hits?.length > 0) { rulesUsage = ruleResults.hits.hits.reduce((usage, hit) => { - const isElastic = isElasticRule(hit._source.alert.tags); - const isEnabled = hit._source.alert.enabled; + // @ts-expect-error _source is optional + const isElastic = isElasticRule(hit._source?.alert.tags); + const isEnabled = Boolean(hit._source?.alert.enabled); return updateRulesUsage({ isElastic, isEnabled }, usage); }, initialRulesUsage); diff --git a/x-pack/plugins/spaces/server/usage_collection/spaces_usage_collector.ts b/x-pack/plugins/spaces/server/usage_collection/spaces_usage_collector.ts index c0cf71fab05584..ef001edd429fd6 100644 --- a/x-pack/plugins/spaces/server/usage_collection/spaces_usage_collector.ts +++ b/x-pack/plugins/spaces/server/usage_collection/spaces_usage_collector.ts @@ -50,6 +50,7 @@ async function getSpacesUsage( let resp: SpacesAggregationResponse | undefined; try { + // @ts-expect-error `SearchResponse['hits']['total']` incorrectly expects `number` type instead of `{ value: number }`. ({ body: resp } = await esClient.search({ index: kibanaIndex, body: { diff --git a/x-pack/plugins/stack_alerts/common/build_sorted_events_query.ts b/x-pack/plugins/stack_alerts/common/build_sorted_events_query.ts index a4fb54a06ace82..34851073bd8a2f 100644 --- a/x-pack/plugins/stack_alerts/common/build_sorted_events_query.ts +++ b/x-pack/plugins/stack_alerts/common/build_sorted_events_query.ts @@ -4,18 +4,21 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; +import type { ESSearchRequest } from '../../../../typings/elasticsearch'; -import { ESSearchBody, ESSearchRequest } from '../../../../typings/elasticsearch'; -import { SortOrder } from '../../../../typings/elasticsearch/aggregations'; - -type BuildSortedEventsQueryOpts = Pick & - Pick, 'index' | 'size'>; +interface BuildSortedEventsQueryOpts { + aggs?: Record; + track_total_hits: boolean | number; + index: estypes.Indices; + size: number; +} export interface BuildSortedEventsQuery extends BuildSortedEventsQueryOpts { filter: unknown; from: string; to: string; - sortOrder?: SortOrder | undefined; + sortOrder?: 'asc' | 'desc'; searchAfterSortId: string | number | undefined; timeField: string; } diff --git a/x-pack/plugins/stack_alerts/public/alert_types/es_query/expression.tsx b/x-pack/plugins/stack_alerts/public/alert_types/es_query/expression.tsx index 09487f1ebe9365..f89f1133d0248b 100644 --- a/x-pack/plugins/stack_alerts/public/alert_types/es_query/expression.tsx +++ b/x-pack/plugins/stack_alerts/public/alert_types/es_query/expression.tsx @@ -23,6 +23,8 @@ import { EuiLink, } from '@elastic/eui'; import { DocLinksStart, HttpSetup } from 'kibana/public'; +import type { estypes } from '@elastic/elasticsearch'; + import { XJson } from '../../../../../../src/plugins/es_ui_shared/public'; import { useKibana } from '../../../../../../src/plugins/kibana_react/public'; import { @@ -39,6 +41,10 @@ import { buildSortedEventsQuery } from '../../../common/build_sorted_events_quer import { EsQueryAlertParams } from './types'; import { IndexSelectPopover } from '../components/index_select_popover'; +function totalHitsToNumber(total: estypes.HitsMetadata['total']): number { + return typeof total === 'number' ? total : total.value; +} + const DEFAULT_VALUES = { THRESHOLD_COMPARATOR: COMPARATORS.GREATER_THAN, QUERY: `{ @@ -191,7 +197,7 @@ export const EsQueryAlertTypeExpression: React.FunctionComponent< setTestQueryResult( i18n.translate('xpack.stackAlerts.esQuery.ui.numQueryMatchesText', { defaultMessage: 'Query matched {count} documents in the last {window}.', - values: { count: hits.total, window }, + values: { count: totalHitsToNumber(hits.total), window }, }) ); } catch (err) { diff --git a/x-pack/plugins/stack_alerts/server/alert_types/es_query/action_context.ts b/x-pack/plugins/stack_alerts/server/alert_types/es_query/action_context.ts index f0596a9fcb9644..bc5d5c41c5cce8 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/es_query/action_context.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/es_query/action_context.ts @@ -6,9 +6,9 @@ */ import { i18n } from '@kbn/i18n'; +import type { estypes } from '@elastic/elasticsearch'; import { AlertExecutorOptions, AlertInstanceContext } from '../../../../alerting/server'; import { EsQueryAlertParams } from './alert_type_params'; -import { ESSearchHit } from '../../../../../../typings/elasticsearch'; // alert type context provided to actions @@ -29,7 +29,7 @@ export interface EsQueryAlertActionContext extends AlertInstanceContext { // threshold conditions conditions: string; // query matches - hits: ESSearchHit[]; + hits: estypes.Hit[]; } export function addMessages( diff --git a/x-pack/plugins/stack_alerts/server/alert_types/es_query/alert_type.test.ts b/x-pack/plugins/stack_alerts/server/alert_types/es_query/alert_type.test.ts index 66984e46de6027..92f6e731f7114a 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/es_query/alert_type.test.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/es_query/alert_type.test.ts @@ -135,6 +135,7 @@ describe('alertType', () => { const searchResult: ESSearchResponse = generateResults([]); alertServices.scopedClusterClient.asCurrentUser.search.mockResolvedValueOnce( + // @ts-expect-error not compatible agregations type elasticsearchClientMock.createSuccessTransportRequestPromise(searchResult) ); @@ -194,6 +195,7 @@ describe('alertType', () => { }, ]); alertServices.scopedClusterClient.asCurrentUser.search.mockResolvedValueOnce( + // @ts-expect-error not compatible response type elasticsearchClientMock.createSuccessTransportRequestPromise(searchResult) ); @@ -247,6 +249,7 @@ describe('alertType', () => { const newestDocumentTimestamp = previousTimestamp + 1000; alertServices.scopedClusterClient.asCurrentUser.search.mockResolvedValueOnce( + // @ts-expect-error not compatible response type elasticsearchClientMock.createSuccessTransportRequestPromise( generateResults([ { @@ -309,6 +312,7 @@ describe('alertType', () => { const oldestDocumentTimestamp = Date.now(); alertServices.scopedClusterClient.asCurrentUser.search.mockResolvedValueOnce( + // @ts-expect-error not compatible response type elasticsearchClientMock.createSuccessTransportRequestPromise( generateResults([ { @@ -370,6 +374,7 @@ describe('alertType', () => { const oldestDocumentTimestamp = Date.now(); alertServices.scopedClusterClient.asCurrentUser.search.mockResolvedValueOnce( + // @ts-expect-error not compatible response type elasticsearchClientMock.createSuccessTransportRequestPromise( generateResults([ { @@ -413,6 +418,7 @@ describe('alertType', () => { const newestDocumentTimestamp = oldestDocumentTimestamp + 5000; alertServices.scopedClusterClient.asCurrentUser.search.mockResolvedValueOnce( + // @ts-expect-error not compatible response type elasticsearchClientMock.createSuccessTransportRequestPromise( generateResults([ { @@ -458,6 +464,7 @@ describe('alertType', () => { const oldestDocumentTimestamp = Date.now(); alertServices.scopedClusterClient.asCurrentUser.search.mockResolvedValueOnce( + // @ts-expect-error not compatible response type elasticsearchClientMock.createSuccessTransportRequestPromise( generateResults( [ @@ -521,6 +528,7 @@ describe('alertType', () => { const oldestDocumentTimestamp = Date.now(); alertServices.scopedClusterClient.asCurrentUser.search.mockResolvedValueOnce( + // @ts-expect-error not compatible response type elasticsearchClientMock.createSuccessTransportRequestPromise( generateResults( [ diff --git a/x-pack/plugins/stack_alerts/server/alert_types/es_query/alert_type.ts b/x-pack/plugins/stack_alerts/server/alert_types/es_query/alert_type.ts index d1cbeeb46fac04..990ab9c1f60027 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/es_query/alert_type.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/es_query/alert_type.ts @@ -6,6 +6,7 @@ */ import { i18n } from '@kbn/i18n'; +import type { estypes } from '@elastic/elasticsearch'; import { Logger } from 'src/core/server'; import { AlertType, AlertExecutorOptions } from '../../types'; import { ActionContext, EsQueryAlertActionContext, addMessages } from './action_context'; @@ -18,7 +19,6 @@ import { STACK_ALERTS_FEATURE_ID } from '../../../common'; import { ComparatorFns, getHumanReadableComparator } from '../lib'; import { parseDuration } from '../../../../alerting/server'; import { buildSortedEventsQuery } from '../../../common/build_sorted_events_query'; -import { ESSearchHit } from '../../../../../../typings/elasticsearch'; export const ES_QUERY_ID = '.es-query'; @@ -217,7 +217,7 @@ export function getAlertType( const { body: searchResult } = await esClient.search(query); if (searchResult.hits.hits.length > 0) { - const numMatches = searchResult.hits.total.value; + const numMatches = (searchResult.hits.total as estypes.TotalHits).value; logger.debug(`alert ${ES_QUERY_ID}:${alertId} "${name}" query has ${numMatches} matches`); // apply the alert condition @@ -251,7 +251,7 @@ export function getAlertType( // update the timestamp based on the current search results const firstValidTimefieldSort = getValidTimefieldSort( - searchResult.hits.hits.find((hit: ESSearchHit) => getValidTimefieldSort(hit.sort))?.sort + searchResult.hits.hits.find((hit) => getValidTimefieldSort(hit.sort))?.sort ); if (firstValidTimefieldSort) { timestamp = firstValidTimefieldSort; @@ -265,7 +265,7 @@ export function getAlertType( } } -function getValidTimefieldSort(sortValues: Array = []): undefined | string { +function getValidTimefieldSort(sortValues: Array = []): undefined | string { for (const sortValue of sortValues) { const sortDate = tryToParseAsDate(sortValue); if (sortDate) { @@ -273,7 +273,7 @@ function getValidTimefieldSort(sortValues: Array = []): undefin } } } -function tryToParseAsDate(sortValue?: string | number): undefined | string { +function tryToParseAsDate(sortValue?: string | number | null): undefined | string { const sortDate = typeof sortValue === 'string' ? Date.parse(sortValue) : sortValue; if (sortDate && !isNaN(sortDate)) { return new Date(sortDate).toISOString(); diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/es_query_builder.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/es_query_builder.ts index a416056217442e..1e26ea09618d5a 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/es_query_builder.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/es_query_builder.ts @@ -7,8 +7,7 @@ import { ElasticsearchClient } from 'kibana/server'; import { Logger } from 'src/core/server'; -import { ApiResponse } from '@elastic/elasticsearch'; -import { SearchResponse } from 'elasticsearch'; +import type { ApiResponse, estypes } from '@elastic/elasticsearch'; import { Query, IIndexPattern, @@ -110,7 +109,7 @@ export async function executeEsQueryFactory( return async ( gteDateTime: Date | null, ltDateTime: Date | null - ): Promise> | undefined> => { + ): Promise | undefined> => { let esFormattedQuery; if (indexQuery) { const gteEpochDateTime = gteDateTime ? new Date(gteDateTime).getTime() : null; @@ -194,7 +193,7 @@ export async function executeEsQueryFactory( }, }; - let esResult: ApiResponse> | undefined; + let esResult: estypes.SearchResponse | undefined; try { ({ body: esResult } = await esClient.search(esQuery)); } catch (err) { diff --git a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts index 866b25d239db74..15a6564395c165 100644 --- a/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts +++ b/x-pack/plugins/stack_alerts/server/alert_types/geo_containment/geo_containment.ts @@ -7,8 +7,7 @@ import _ from 'lodash'; import { Logger } from 'src/core/server'; -import { ApiResponse } from '@elastic/elasticsearch'; -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { executeEsQueryFactory, getShapesFilters, OTHER_CATEGORY } from './es_query_builder'; import { AlertServices } from '../../../../alerting/server'; import { @@ -23,7 +22,7 @@ export type LatestEntityLocation = GeoContainmentInstanceState; // Flatten agg results and get latest locations for each entity export function transformResults( - results: SearchResponse | undefined, + results: estypes.SearchResponse | undefined, dateField: string, geoField: string ): Map { @@ -164,7 +163,7 @@ export const getGeoContainmentExecutor = (log: Logger): GeoContainmentAlertType[ ); // Start collecting data only on the first cycle - let currentIntervalResults: ApiResponse> | undefined; + let currentIntervalResults: estypes.SearchResponse | undefined; if (!currIntervalStartTime) { log.debug(`alert ${GEO_CONTAINMENT_ID}:${alertId} alert initialized. Collecting data`); // Consider making first time window configurable? @@ -177,6 +176,7 @@ export const getGeoContainmentExecutor = (log: Logger): GeoContainmentAlertType[ } const currLocationMap: Map = transformResults( + // @ts-expect-error body doesn't exist on currentIntervalResults currentIntervalResults?.body, params.dateField, params.geoField diff --git a/x-pack/plugins/task_manager/server/monitoring/workload_statistics.test.ts b/x-pack/plugins/task_manager/server/monitoring/workload_statistics.test.ts index 46d8478a7ecfa9..05b74f4340d095 100644 --- a/x-pack/plugins/task_manager/server/monitoring/workload_statistics.test.ts +++ b/x-pack/plugins/task_manager/server/monitoring/workload_statistics.test.ts @@ -6,78 +6,88 @@ */ import { first, take, bufferCount } from 'rxjs/operators'; -import { loggingSystemMock } from '../../../../../src/core/server/mocks'; +import { loggingSystemMock, elasticsearchServiceMock } from '../../../../../src/core/server/mocks'; import { - WorkloadAggregation, + TaskTypeAggregation, + WorkloadAggregationResponse, + ScheduleDensityHistogram, createWorkloadAggregator, padBuckets, estimateRecurringTaskScheduling, } from './workload_statistics'; import { ConcreteTaskInstance } from '../task'; -import { AggregationResultOf, ESSearchResponse } from '../../../../../typings/elasticsearch'; + import { times } from 'lodash'; import { taskStoreMock } from '../task_store.mock'; import { of, Subject } from 'rxjs'; import { sleep } from '../test_utils'; +import { estypes } from '@elastic/elasticsearch'; + +type ResponseWithAggs = Omit, 'aggregations'> & { + aggregations: WorkloadAggregationResponse; +}; -type MockESResult = ESSearchResponse< - ConcreteTaskInstance, - { - body: WorkloadAggregation; - } ->; +const asApiResponse = (body: ResponseWithAggs) => + elasticsearchServiceMock + .createSuccessTransportRequestPromise(body as estypes.SearchResponse) + .then((res) => res.body as ResponseWithAggs); describe('Workload Statistics Aggregator', () => { test('queries the Task Store at a fixed interval for the current workload', async () => { const taskStore = taskStoreMock.create({}); - taskStore.aggregate.mockResolvedValue({ - hits: { - hits: [], - max_score: 0, - total: { value: 0, relation: 'eq' }, - }, - took: 1, - timed_out: false, - _shards: { - total: 1, - successful: 1, - skipped: 1, - failed: 0, - }, - aggregations: { - taskType: { - buckets: [], - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, + taskStore.aggregate.mockResolvedValue( + asApiResponse({ + hits: { + hits: [], + max_score: 0, + total: { value: 0, relation: 'eq' }, }, - schedule: { - buckets: [], - doc_count_error_upper_bound: 0, - sum_other_doc_count: 0, + took: 1, + timed_out: false, + _shards: { + total: 1, + successful: 1, + skipped: 1, + failed: 0, }, - idleTasks: { - doc_count: 0, - overdue: { - doc_count: 0, + aggregations: { + taskType: { + buckets: [], + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, }, - scheduleDensity: { - buckets: [ - { - key: '2020-10-02T15:18:37.274Z-2020-10-02T15:19:36.274Z', - from: 1.601651917274e12, - from_as_string: '2020-10-02T15:18:37.274Z', - to: 1.601651976274e12, - to_as_string: '2020-10-02T15:19:36.274Z', - doc_count: 0, - histogram: { - buckets: [], + schedule: { + buckets: [], + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + }, + // The `FiltersAggregate` doesn't cover the case of a nested `AggregationContainer`, in which `FiltersAggregate` + // would not have a `buckets` property, but rather a keyed property that's inferred from the request. + // @ts-expect-error + idleTasks: { + doc_count: 0, + overdue: { + doc_count: 0, + }, + scheduleDensity: { + buckets: [ + { + key: '2020-10-02T15:18:37.274Z-2020-10-02T15:19:36.274Z', + from: 1.601651917274e12, + from_as_string: '2020-10-02T15:18:37.274Z', + to: 1.601651976274e12, + to_as_string: '2020-10-02T15:19:36.274Z', + doc_count: 0, + histogram: { + buckets: [], + }, }, - }, - ], + ], + }, }, }, - }, - } as MockESResult); + }) + ); const workloadAggregator = createWorkloadAggregator( taskStore, @@ -146,8 +156,8 @@ describe('Workload Statistics Aggregator', () => { }); }); - const mockAggregatedResult: () => MockESResult = () => - ({ + const mockAggregatedResult = () => + asApiResponse({ hits: { hits: [], max_score: 0, @@ -228,6 +238,9 @@ describe('Workload Statistics Aggregator', () => { }, ], }, + // The `FiltersAggregate` doesn't cover the case of a nested `AggregationContainer`, in which `FiltersAggregate` + // would not have a `buckets` property, but rather a keyed property that's inferred from the request. + // @ts-expect-error idleTasks: { doc_count: 13, overdue: { @@ -240,7 +253,7 @@ describe('Workload Statistics Aggregator', () => { }, }, }, - } as MockESResult); + }); test('returns a summary of the workload by task type', async () => { const taskStore = taskStoreMock.create({}); @@ -440,16 +453,20 @@ describe('Workload Statistics Aggregator', () => { const taskStore = taskStoreMock.create({}); taskStore.aggregate .mockResolvedValueOnce( - setTaskTypeCount(mockAggregatedResult(), 'alerting_telemetry', { - idle: 2, - }) + mockAggregatedResult().then((res) => + setTaskTypeCount(res, 'alerting_telemetry', { + idle: 2, + }) + ) ) .mockRejectedValueOnce(new Error('Elasticsearch has gone poof')) .mockResolvedValueOnce( - setTaskTypeCount(mockAggregatedResult(), 'alerting_telemetry', { - idle: 1, - failed: 1, - }) + mockAggregatedResult().then((res) => + setTaskTypeCount(res, 'alerting_telemetry', { + idle: 1, + failed: 1, + }) + ) ); const logger = loggingSystemMock.create().get(); const workloadAggregator = createWorkloadAggregator(taskStore, of(true), 10, 3000, logger); @@ -502,7 +519,7 @@ describe('Workload Statistics Aggregator', () => { reject(new Error(`Elasticsearch is still poof`)); } - return setTaskTypeCount(mockAggregatedResult(), 'alerting_telemetry', { + return setTaskTypeCount(await mockAggregatedResult(), 'alerting_telemetry', { idle: 2, }); }); @@ -631,6 +648,7 @@ describe('padBuckets', () => { padBuckets(10, 3000, { key: '2020-10-02T19:47:28.128Z-2020-10-02T19:48:28.128Z', from: 1601668048128, + // @ts-expect-error @elastic/elasticsearch doesn't decalre from_as_string property from_as_string: '2020-10-02T19:47:28.128Z', to: 1601668108128, to_as_string: '2020-10-02T19:48:28.128Z', @@ -647,6 +665,7 @@ describe('padBuckets', () => { padBuckets(10, 3000, { key: '2020-10-02T19:47:28.128Z-2020-10-02T19:48:28.128Z', from: 1601668046000, + // @ts-expect-error @elastic/elasticsearch doesn't decalre from_as_string property from_as_string: '2020-10-02T19:47:26.000Z', to: 1601668076000, to_as_string: '2020-10-02T19:47:56.000Z', @@ -724,6 +743,7 @@ describe('padBuckets', () => { padBuckets(10, 3000, { key: '2020-10-02T20:39:45.793Z-2020-10-02T20:40:14.793Z', from: 1601671183000, + // @ts-expect-error @elastic/elasticsearch doesn't decalre from_as_string property from_as_string: '2020-10-02T20:39:43.000Z', to: 1601671213000, to_as_string: '2020-10-02T20:40:13.000Z', @@ -753,6 +773,7 @@ describe('padBuckets', () => { padBuckets(20, 3000, { key: '2020-10-02T20:39:45.793Z-2020-10-02T20:40:14.793Z', from: 1601671185793, + // @ts-expect-error @elastic/elasticsearch doesn't decalre from_as_string property from_as_string: '2020-10-02T20:39:45.793Z', to: 1601671245793, to_as_string: '2020-10-02T20:40:45.793Z', @@ -782,6 +803,7 @@ describe('padBuckets', () => { padBuckets(20, 3000, { key: '2021-02-02T10:08:32.161Z-2021-02-02T10:09:32.161Z', from: 1612260512161, + // @ts-expect-error @elastic/elasticsearch doesn't decalre from_as_string property from_as_string: '2021-02-02T10:08:32.161Z', to: 1612260572161, to_as_string: '2021-02-02T10:09:32.161Z', @@ -898,16 +920,12 @@ describe('padBuckets', () => { }); function setTaskTypeCount( - { aggregations }: MockESResult, + { aggregations, ...rest }: ResponseWithAggs, taskType: string, status: Record ) { - const taskTypes = aggregations!.taskType as AggregationResultOf< - WorkloadAggregation['aggs']['taskType'], - {} - >; const buckets = [ - ...taskTypes.buckets.filter(({ key }) => key !== taskType), + ...(aggregations.taskType as TaskTypeAggregation).buckets.filter(({ key }) => key !== taskType), { key: taskType, doc_count: Object.values(status).reduce((sum, count) => sum + count, 0), @@ -920,9 +938,14 @@ function setTaskTypeCount( }, }, ]; - return ({ + return { + ...rest, hits: { - total: { value: buckets.reduce((sum, bucket) => sum + bucket.doc_count, 0) }, + ...rest.hits, + total: { + value: buckets.reduce((sum, bucket) => sum + bucket.doc_count, 0), + relation: 'eq' as estypes.TotalHitsRelation, + }, }, aggregations: { ...aggregations, @@ -931,7 +954,7 @@ function setTaskTypeCount( buckets, }, }, - } as {}) as MockESResult; + }; } /** * @@ -951,7 +974,7 @@ function mockHistogram( to: number, interval: number, foundBuckets: Array -) { +): ScheduleDensityHistogram { const now = Date.now(); const fromDate = new Date(now + from); const toDate = new Date(now + to); diff --git a/x-pack/plugins/task_manager/server/monitoring/workload_statistics.ts b/x-pack/plugins/task_manager/server/monitoring/workload_statistics.ts index 08850c86505191..c79b310822c3ec 100644 --- a/x-pack/plugins/task_manager/server/monitoring/workload_statistics.ts +++ b/x-pack/plugins/task_manager/server/monitoring/workload_statistics.ts @@ -10,6 +10,7 @@ import { mergeMap, map, filter, switchMap, catchError } from 'rxjs/operators'; import { Logger } from 'src/core/server'; import { JsonObject } from 'src/plugins/kibana_utils/common'; import { keyBy, mapValues } from 'lodash'; +import { estypes } from '@elastic/elasticsearch'; import { AggregatedStatProvider } from './runtime_statistics_aggregator'; import { parseIntervalAsSecond, asInterval, parseIntervalAsMillisecond } from '../lib/intervals'; import { AggregationResultOf } from '../../../../../typings/elasticsearch'; @@ -85,9 +86,11 @@ export interface WorkloadAggregation { // The type of a bucket in the scheduleDensity range aggregation type ScheduleDensityResult = AggregationResultOf< + // @ts-expect-error AggregationRange reqires from: number WorkloadAggregation['aggs']['idleTasks']['aggs']['scheduleDensity'], {} >['buckets'][0]; +// @ts-expect-error cannot infer histogram type ScheduledIntervals = ScheduleDensityResult['histogram']['buckets'][0]; // Set an upper bound just in case a customer sets a really high refresh rate @@ -134,7 +137,9 @@ export function createWorkloadAggregator( field: 'task.runAt', ranges: [ { + // @ts-expect-error @elastic/elasticsearch The `AggregationRange` type only supports `double` for `from` and `to` but it can be a string too for time based ranges from: `now`, + // @ts-expect-error @elastic/elasticsearch The `AggregationRange` type only supports `double` for `from` and `to` but it can be a string too for time based ranges to: `now+${asInterval(scheduleDensityBuckets * pollInterval)}`, }, ], @@ -170,19 +175,11 @@ export function createWorkloadAggregator( map((result) => { const { aggregations, - hits: { - total: { value: count }, - }, + hits: { total }, } = result; + const count = typeof total === 'number' ? total : total.value; - if ( - !( - aggregations?.taskType && - aggregations?.schedule && - aggregations?.idleTasks?.overdue && - aggregations?.idleTasks?.scheduleDensity - ) - ) { + if (!hasAggregations(aggregations)) { throw new Error(`Invalid workload: ${JSON.stringify(result)}`); } @@ -240,7 +237,9 @@ export function padBuckets( pollInterval: number, scheduleDensity: ScheduleDensityResult ): number[] { + // @ts-expect-error cannot infer histogram if (scheduleDensity.from && scheduleDensity.to && scheduleDensity.histogram?.buckets?.length) { + // @ts-expect-error cannot infer histogram const { histogram, from, to } = scheduleDensity; const firstBucket = histogram.buckets[0].key; const lastBucket = histogram.buckets[histogram.buckets.length - 1].key; @@ -354,3 +353,84 @@ export function summarizeWorkloadStat( status: HealthStatus.OK, }; } + +function hasAggregations( + aggregations?: Record +): aggregations is WorkloadAggregationResponse { + return !!( + aggregations?.taskType && + aggregations?.schedule && + (aggregations?.idleTasks as IdleTasksAggregation)?.overdue && + (aggregations?.idleTasks as IdleTasksAggregation)?.scheduleDensity + ); +} +export interface WorkloadAggregationResponse { + taskType: TaskTypeAggregation; + schedule: ScheduleAggregation; + idleTasks: IdleTasksAggregation; + [otherAggs: string]: estypes.Aggregate; +} +export interface TaskTypeAggregation extends estypes.FiltersAggregate { + buckets: Array<{ + doc_count: number; + key: string | number; + status: { + buckets: Array<{ + doc_count: number; + key: string | number; + }>; + doc_count_error_upper_bound?: number | undefined; + sum_other_doc_count?: number | undefined; + }; + }>; + doc_count_error_upper_bound?: number | undefined; + sum_other_doc_count?: number | undefined; +} +export interface ScheduleAggregation extends estypes.FiltersAggregate { + buckets: Array<{ + doc_count: number; + key: string | number; + }>; + doc_count_error_upper_bound?: number | undefined; + sum_other_doc_count?: number | undefined; +} + +export type ScheduleDensityHistogram = DateRangeBucket & { + histogram: { + buckets: Array< + DateHistogramBucket & { + interval: { + buckets: Array<{ + doc_count: number; + key: string | number; + }>; + doc_count_error_upper_bound?: number | undefined; + sum_other_doc_count?: number | undefined; + }; + } + >; + }; +}; +export interface IdleTasksAggregation extends estypes.FiltersAggregate { + doc_count: number; + scheduleDensity: { + buckets: ScheduleDensityHistogram[]; + }; + overdue: { + doc_count: number; + }; +} + +interface DateHistogramBucket { + doc_count: number; + key: number; + key_as_string: string; +} +interface DateRangeBucket { + key: string; + to?: number; + from?: number; + to_as_string?: string; + from_as_string?: string; + doc_count: number; +} diff --git a/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.test.ts b/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.test.ts index 57a4ab320367d4..9e31ab9f0cb4e2 100644 --- a/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.test.ts +++ b/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.test.ts @@ -6,7 +6,7 @@ */ import _ from 'lodash'; -import { asUpdateByQuery, shouldBeOneOf, mustBeAllOf } from './query_clauses'; +import { shouldBeOneOf, mustBeAllOf } from './query_clauses'; import { updateFieldsAndMarkAsFailed, @@ -41,25 +41,23 @@ describe('mark_available_tasks_as_claimed', () => { retryAt: claimOwnershipUntil, }; - expect( - asUpdateByQuery({ - query: mustBeAllOf( - // Either a task with idle status and runAt <= now or - // status running or claiming with a retryAt <= now. - shouldBeOneOf(IdleTaskWithExpiredRunAt, RunningOrClaimingTaskWithExpiredRetryAt) - ), - update: updateFieldsAndMarkAsFailed( - fieldUpdates, - claimTasksById || [], - definitions.getAllTypes(), - [], - Array.from(definitions).reduce((accumulator, [type, { maxAttempts }]) => { - return { ...accumulator, [type]: maxAttempts || defaultMaxAttempts }; - }, {}) - ), - sort: SortByRunAtAndRetryAt, - }) - ).toEqual({ + expect({ + query: mustBeAllOf( + // Either a task with idle status and runAt <= now or + // status running or claiming with a retryAt <= now. + shouldBeOneOf(IdleTaskWithExpiredRunAt, RunningOrClaimingTaskWithExpiredRetryAt) + ), + script: updateFieldsAndMarkAsFailed( + fieldUpdates, + claimTasksById || [], + definitions.getAllTypes(), + [], + Array.from(definitions).reduce((accumulator, [type, { maxAttempts }]) => { + return { ...accumulator, [type]: maxAttempts || defaultMaxAttempts }; + }, {}) + ), + sort: SortByRunAtAndRetryAt, + }).toEqual({ query: { bool: { must: [ @@ -114,7 +112,6 @@ if (doc['task.runAt'].size()!=0) { }, }, }, - seq_no_primary_term: true, script: { source: ` if (params.claimableTaskTypes.contains(ctx._source.task.taskType)) { diff --git a/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts b/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts index 8598980a4e2363..2437e893abf373 100644 --- a/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts +++ b/x-pack/plugins/task_manager/server/queries/mark_available_tasks_as_claimed.ts @@ -4,27 +4,16 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import { - SortClause, + ScriptBasedSortClause, ScriptClause, - ExistsFilter, - TermFilter, - RangeFilter, mustBeAllOf, MustCondition, - BoolClauseWithAnyCondition, - ShouldCondition, - FilterCondition, + MustNotCondition, } from './query_clauses'; -export const TaskWithSchedule: ExistsFilter = { - exists: { field: 'task.schedule' }, -}; -export function taskWithLessThanMaxAttempts( - type: string, - maxAttempts: number -): MustCondition { +export function taskWithLessThanMaxAttempts(type: string, maxAttempts: number): MustCondition { return { bool: { must: [ @@ -41,7 +30,7 @@ export function taskWithLessThanMaxAttempts( }; } -export function tasksOfType(taskTypes: string[]): ShouldCondition { +export function tasksOfType(taskTypes: string[]): estypes.QueryContainer { return { bool: { should: [...taskTypes].map((type) => ({ term: { 'task.taskType': type } })), @@ -51,7 +40,7 @@ export function tasksOfType(taskTypes: string[]): ShouldCondition { export function tasksClaimedByOwner( taskManagerId: string, - ...taskFilters: Array | ShouldCondition> + ...taskFilters: estypes.QueryContainer[] ) { return mustBeAllOf( { @@ -64,15 +53,13 @@ export function tasksClaimedByOwner( ); } -export const IdleTaskWithExpiredRunAt: MustCondition = { +export const IdleTaskWithExpiredRunAt: MustCondition = { bool: { must: [{ term: { 'task.status': 'idle' } }, { range: { 'task.runAt': { lte: 'now' } } }], }, }; -// TODO: Fix query clauses to support this -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export const InactiveTasks: BoolClauseWithAnyCondition = { +export const InactiveTasks: MustNotCondition = { bool: { must_not: [ { @@ -85,7 +72,7 @@ export const InactiveTasks: BoolClauseWithAnyCondition = { }, }; -export const RunningOrClaimingTaskWithExpiredRetryAt: MustCondition = { +export const RunningOrClaimingTaskWithExpiredRetryAt: MustCondition = { bool: { must: [ { @@ -98,7 +85,7 @@ export const RunningOrClaimingTaskWithExpiredRetryAt: MustCondition; export const updateFieldsAndMarkAsFailed = ( fieldUpdates: { diff --git a/x-pack/plugins/task_manager/server/queries/query_clauses.test.ts b/x-pack/plugins/task_manager/server/queries/query_clauses.test.ts index f144c8921263fd..5f99682718f1dd 100644 --- a/x-pack/plugins/task_manager/server/queries/query_clauses.test.ts +++ b/x-pack/plugins/task_manager/server/queries/query_clauses.test.ts @@ -6,29 +6,21 @@ */ import _ from 'lodash'; -import { - MustCondition, - shouldBeOneOf, - mustBeAllOf, - ExistsFilter, - TermFilter, - RangeFilter, - matchesClauses, -} from './query_clauses'; +import { MustCondition, shouldBeOneOf, mustBeAllOf, matchesClauses } from './query_clauses'; describe('matchesClauses', () => { test('merges multiple types of Bool Clauses into one', () => { - const TaskWithSchedule: ExistsFilter = { + const TaskWithSchedule = { exists: { field: 'task.schedule' }, }; - const IdleTaskWithExpiredRunAt: MustCondition = { + const IdleTaskWithExpiredRunAt: MustCondition = { bool: { must: [{ term: { 'task.status': 'idle' } }, { range: { 'task.runAt': { lte: 'now' } } }], }, }; - const RunningTask: MustCondition = { + const RunningTask: MustCondition = { bool: { must: [{ term: { 'task.status': 'running' } }], }, @@ -37,10 +29,7 @@ describe('matchesClauses', () => { expect( matchesClauses( mustBeAllOf(TaskWithSchedule), - shouldBeOneOf( - RunningTask, - IdleTaskWithExpiredRunAt - ) + shouldBeOneOf(RunningTask, IdleTaskWithExpiredRunAt) ) ).toMatchObject({ bool: { diff --git a/x-pack/plugins/task_manager/server/queries/query_clauses.ts b/x-pack/plugins/task_manager/server/queries/query_clauses.ts index 4d59bec4e36255..97c41295eece32 100644 --- a/x-pack/plugins/task_manager/server/queries/query_clauses.ts +++ b/x-pack/plugins/task_manager/server/queries/query_clauses.ts @@ -5,167 +5,27 @@ * 2.0. */ -/** - * Terminology - * =========== - * The terms for the differenct clauses in an Elasticsearch query can be confusing, here are some - * clarifications that might help you understand the Typescript types we use here. - * - * Given the following Query: - * { - * "query": { (1) - * "bool": { (2) - * "must": - * [ - * (3) { "term" : { "tag" : "wow" } }, - * { "term" : { "tag" : "elasticsearch" } }, - * { - * "must" : { "term" : { "user" : "kimchy" } } - * } - * ] - * } - * } - * } - * - * These are referred to as: - * (1). BoolClause / BoolClauseWithAnyCondition - * (2). BoolCondition / AnyBoolCondition - * (3). BoolClauseFilter - * - */ - -export interface TermFilter { - term: { [field: string]: string | string[] }; -} -export interface RangeFilter { - range: { - [field: string]: { lte: string | number } | { lt: string | number } | { gt: string | number }; - }; -} -export interface ExistsFilter { - exists: { field: string }; -} - -type BoolClauseFilter = TermFilter | RangeFilter | ExistsFilter; -type BoolClauseFiltering = - | BoolClauseWithAnyCondition - | PinnedQuery - | T; +import { estypes } from '@elastic/elasticsearch'; -enum Conditions { - Should = 'should', - Must = 'must', - MustNot = 'must_not', - Filter = 'filter', +export interface MustCondition { + bool: Pick; } - -/** - * Describe a specific BoolClause Condition with a BoolClauseFilter on it, such as: - * ``` - * { - * must : [ - * T, ... - * ] - * } - * ``` - */ -type BoolCondition = { - [c in C]: Array>; -}; - -/** - * Describe a Bool clause with a specific Condition, such as: - * ``` - * { - * // described by BoolClause - * bool: { - * // described by BoolCondition - * must: [ - * T, ... - * ] - * } - * } - * ``` - */ -interface BoolClause { - bool: BoolCondition; +export interface MustNotCondition { + bool: Pick; } -/** - * Describe a Bool clause with mixed Conditions, such as: - * ``` - * { - * // described by BoolClause<...> - * bool: { - * // described by BoolCondition - * must : { - * ... - * }, - * // described by BoolCondition - * should : { - * ... - * } - * } - * } - * ``` - */ -type AnyBoolCondition = { - [Condition in Conditions]?: Array>; -}; - -/** - * Describe a Bool Condition with any Condition on it, so it can handle both: - * ``` - * { - * bool: { - * must : { - * ... - * } - * } - * } - * ``` - * - * and: - * - * ``` - * { - * bool: { - * must_not : { - * ... - * } - * } - * } - * ``` - */ -export interface BoolClauseWithAnyCondition { - bool: AnyBoolCondition; -} - -/** - * Describe the various Bool Clause Conditions we support, as specified in the Conditions enum - */ -export type ShouldCondition = BoolClause; -export type MustCondition = BoolClause; -export type MustNotCondition = BoolClause; -export type FilterCondition = BoolClause; - -export interface SortClause { +export interface ScriptBasedSortClause { _script: { type: string; order: string; - script: { - lang: string; - source: string; - params?: { [param: string]: string | string[] }; - }; + script: ScriptClause; }; } -export type SortOptions = string | SortClause | Array; export interface ScriptClause { source: string; lang: string; - params: { + params?: { [field: string]: | string | number @@ -175,33 +35,16 @@ export interface ScriptClause { }; } -export interface UpdateByQuery { - query: PinnedQuery | BoolClauseWithAnyCondition; - sort: SortOptions; - seq_no_primary_term: true; - script: ScriptClause; -} - -export interface PinnedQuery { - pinned: PinnedClause; -} +export type PinnedQuery = Pick; -export interface PinnedClause { - ids: string[]; - organic: BoolClauseWithAnyCondition; -} - -export function matchesClauses( - ...clauses: Array> -): BoolClauseWithAnyCondition { +type BoolClause = Pick; +export function matchesClauses(...clauses: BoolClause[]): BoolClause { return { bool: Object.assign({}, ...clauses.map((clause) => clause.bool)), }; } -export function shouldBeOneOf( - ...should: Array> -): ShouldCondition { +export function shouldBeOneOf(...should: estypes.QueryContainer[]) { return { bool: { should, @@ -209,9 +52,7 @@ export function shouldBeOneOf( }; } -export function mustBeAllOf( - ...must: Array> -): MustCondition { +export function mustBeAllOf(...must: estypes.QueryContainer[]) { return { bool: { must, @@ -219,9 +60,7 @@ export function mustBeAllOf( }; } -export function filterDownBy( - ...filter: Array> -): FilterCondition { +export function filterDownBy(...filter: estypes.QueryContainer[]) { return { bool: { filter, @@ -229,10 +68,10 @@ export function filterDownBy( }; } -export function asPinnedQuery( - ids: PinnedClause['ids'], - organic: PinnedClause['organic'] -): PinnedQuery { +export function asPinnedQuery( + ids: estypes.PinnedQuery['ids'], + organic: estypes.PinnedQuery['organic'] +): Pick { return { pinned: { ids, @@ -240,20 +79,3 @@ export function asPinnedQuery( }, }; } - -export function asUpdateByQuery({ - query, - update, - sort, -}: { - query: UpdateByQuery['query']; - update: UpdateByQuery['script']; - sort: UpdateByQuery['sort']; -}): UpdateByQuery { - return { - query, - sort, - seq_no_primary_term: true, - script: update, - }; -} diff --git a/x-pack/plugins/task_manager/server/queries/task_claiming.test.ts b/x-pack/plugins/task_manager/server/queries/task_claiming.test.ts index bd1171d7fd2f82..8f1332ccd1f9f7 100644 --- a/x-pack/plugins/task_manager/server/queries/task_claiming.test.ts +++ b/x-pack/plugins/task_manager/server/queries/task_claiming.test.ts @@ -15,7 +15,7 @@ import { SearchOpts, StoreOpts, UpdateByQueryOpts, UpdateByQuerySearchOpts } fro import { asTaskClaimEvent, ClaimTaskErr, TaskClaimErrorType, TaskEvent } from '../task_events'; import { asOk, asErr } from '../lib/result_type'; import { TaskTypeDictionary } from '../task_type_dictionary'; -import { BoolClauseWithAnyCondition, TermFilter } from '../queries/query_clauses'; +import type { MustNotCondition } from '../queries/query_clauses'; import { mockLogger } from '../test_utils'; import { TaskClaiming, OwnershipClaimingOpts, TaskClaimingOpts } from './task_claiming'; import { Observable } from 'rxjs'; @@ -177,7 +177,7 @@ describe('TaskClaiming', () => { result, args: { search: store.fetch.mock.calls[index][0] as SearchOpts & { - query: BoolClauseWithAnyCondition; + query: MustNotCondition; }, updateByQuery: store.updateByQuery.mock.calls[index] as [ UpdateByQuerySearchOpts, @@ -767,12 +767,12 @@ if (doc['task.runAt'].size()!=0) { ).map( (result, index) => (store.updateByQuery.mock.calls[index][0] as { - query: BoolClauseWithAnyCondition; + query: MustNotCondition; size: number; sort: string | string[]; script: { params: { - claimableTaskTypes: string[]; + [claimableTaskTypes: string]: string[]; }; }; }).script.params.claimableTaskTypes diff --git a/x-pack/plugins/task_manager/server/queries/task_claiming.ts b/x-pack/plugins/task_manager/server/queries/task_claiming.ts index b4e11dbf81eb10..dce78242816583 100644 --- a/x-pack/plugins/task_manager/server/queries/task_claiming.ts +++ b/x-pack/plugins/task_manager/server/queries/task_claiming.ts @@ -27,13 +27,11 @@ import { } from '../task_events'; import { - asUpdateByQuery, shouldBeOneOf, mustBeAllOf, filterDownBy, asPinnedQuery, matchesClauses, - SortOptions, } from './query_clauses'; import { @@ -50,6 +48,7 @@ import { correctVersionConflictsForContinuation, TaskStore, UpdateByQueryResult, + SearchOpts, } from '../task_store'; import { FillPoolResult } from '../lib/fill_pool'; @@ -375,21 +374,21 @@ export class TaskClaiming { // the score seems to favor newer documents rather than older documents, so // if there are not pinned tasks being queried, we do NOT want to sort by score // at all, just by runAt/retryAt. - const sort: SortOptions = [SortByRunAtAndRetryAt]; + const sort: NonNullable = [SortByRunAtAndRetryAt]; if (claimTasksById && claimTasksById.length) { sort.unshift('_score'); } const apmTrans = apm.startTransaction(`taskManager markAvailableTasksAsClaimed`, 'taskManager'); const result = await this.taskStore.updateByQuery( - asUpdateByQuery({ + { query: matchesClauses( claimTasksById && claimTasksById.length ? mustBeAllOf(asPinnedQuery(claimTasksById, queryForScheduledTasks)) : queryForScheduledTasks, filterDownBy(InactiveTasks) ), - update: updateFieldsAndMarkAsFailed( + script: updateFieldsAndMarkAsFailed( { ownerId: this.taskStore.taskManagerId, retryAt: claimOwnershipUntil, @@ -400,7 +399,7 @@ export class TaskClaiming { pick(this.taskMaxAttempts, taskTypesToClaim) ), sort, - }), + }, { max_docs: size, } diff --git a/x-pack/plugins/task_manager/server/task_store.test.ts b/x-pack/plugins/task_manager/server/task_store.test.ts index 25ee8cb0e23745..a44bddcdb82014 100644 --- a/x-pack/plugins/task_manager/server/task_store.test.ts +++ b/x-pack/plugins/task_manager/server/task_store.test.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import _ from 'lodash'; import { first } from 'rxjs/operators'; @@ -24,7 +24,6 @@ import { SavedObjectsErrorHelpers, } from 'src/core/server'; import { TaskTypeDictionary } from './task_type_dictionary'; -import { RequestEvent } from '@elastic/elasticsearch/lib/Transport'; import { mockLogger } from './test_utils'; const savedObjectsClient = savedObjectsRepositoryMock.create(); @@ -206,8 +205,8 @@ describe('TaskStore', () => { }); }); - async function testFetch(opts?: SearchOpts, hits: unknown[] = []) { - esClient.search.mockResolvedValue(asApiResponse({ hits: { hits } })); + async function testFetch(opts?: SearchOpts, hits: Array> = []) { + esClient.search.mockResolvedValue(asApiResponse({ hits: { hits, total: hits.length } })); const result = await store.fetch(opts); @@ -564,9 +563,17 @@ describe('TaskStore', () => { }); }); -const asApiResponse = (body: T): RequestEvent => - ({ - body, - } as RequestEvent); +const asApiResponse = (body: Pick) => + elasticsearchServiceMock.createSuccessTransportRequestPromise({ + hits: body.hits, + took: 0, + timed_out: false, + _shards: { + failed: 0, + successful: body.hits.hits.length, + total: 0, + skipped: 0, + }, + }); const randomId = () => `id-${_.random(1, 20)}`; diff --git a/x-pack/plugins/task_manager/server/task_store.ts b/x-pack/plugins/task_manager/server/task_store.ts index 083ce1507e6e5a..af0adad43baa46 100644 --- a/x-pack/plugins/task_manager/server/task_store.ts +++ b/x-pack/plugins/task_manager/server/task_store.ts @@ -10,7 +10,9 @@ */ import { Subject } from 'rxjs'; import { omit, defaults } from 'lodash'; -import { ReindexResponseBase, SearchResponse, UpdateDocumentByQueryResponse } from 'elasticsearch'; + +import type { estypes } from '@elastic/elasticsearch'; + import { SavedObject, SavedObjectsSerializer, @@ -31,7 +33,6 @@ import { } from './task'; import { TaskTypeDictionary } from './task_type_dictionary'; -import { ESSearchResponse, ESSearchBody } from '../../../../typings/elasticsearch'; export interface StoreOpts { esClient: ElasticsearchClient; @@ -43,18 +44,21 @@ export interface StoreOpts { } export interface SearchOpts { - sort?: string | object | object[]; - query?: object; + search_after?: Array; size?: number; + sort?: estypes.Sort; + query?: estypes.QueryContainer; seq_no_primary_term?: boolean; - search_after?: unknown[]; } -export type AggregationOpts = Pick, 'aggs'> & - Pick; +export interface AggregationOpts { + aggs: Record; + query?: estypes.QueryContainer; + size?: number; +} export interface UpdateByQuerySearchOpts extends SearchOpts { - script?: object; + script?: estypes.Script; } export interface UpdateByQueryOpts extends SearchOpts { @@ -304,7 +308,7 @@ export class TaskStore { body: { hits: { hits: tasks }, }, - } = await this.esClient.search>({ + } = await this.esClient.search({ index: this.index, ignore_unavailable: true, body: { @@ -315,7 +319,9 @@ export class TaskStore { return { docs: tasks + // @ts-expect-error @elastic/elasticsearch `Hid._id` expected to be `string` .filter((doc) => this.serializer.isRawSavedObject(doc)) + // @ts-expect-error @elastic/elasticsearch `Hid._id` expected to be `string` .map((doc) => this.serializer.rawToSavedObject(doc)) .map((doc) => omit(doc, 'namespace') as SavedObject) .map(savedObjectToConcreteTaskInstance), @@ -330,10 +336,8 @@ export class TaskStore { aggs, query, size = 0, - }: TSearchRequest): Promise> { - const { body } = await this.esClient.search< - ESSearchResponse - >({ + }: TSearchRequest): Promise> { + const { body } = await this.esClient.search({ index: this.index, ignore_unavailable: true, body: ensureAggregationOnlyReturnsTaskObjects({ @@ -355,14 +359,14 @@ export class TaskStore { const { // eslint-disable-next-line @typescript-eslint/naming-convention body: { total, updated, version_conflicts }, - } = await this.esClient.updateByQuery({ + } = await this.esClient.updateByQuery({ index: this.index, ignore_unavailable: true, refresh: true, - max_docs, conflicts: 'proceed', body: { ...opts, + max_docs, query, }, }); @@ -374,7 +378,9 @@ export class TaskStore { ); return { + // @ts-expect-error @elastic/elasticsearch declares UpdateByQueryResponse.total as optional total, + // @ts-expect-error @elastic/elasticsearch declares UpdateByQueryResponse.total as optional updated, version_conflicts: conflictsCorrectedForContinuation, }; @@ -393,11 +399,13 @@ export class TaskStore { * `max_docs`, but we bias in favour of over zealous `version_conflicts` as that's the best indicator we * have for an unhealthy cluster distribution of Task Manager polling intervals */ + export function correctVersionConflictsForContinuation( - updated: ReindexResponseBase['updated'], - versionConflicts: ReindexResponseBase['version_conflicts'], + updated: estypes.ReindexResponse['updated'], + versionConflicts: estypes.ReindexResponse['version_conflicts'], maxDocs?: number -) { +): number { + // @ts-expect-error estypes.ReindexResponse['updated'] and estypes.ReindexResponse['version_conflicts'] can be undefined return maxDocs && versionConflicts + updated > maxDocs ? maxDocs - updated : versionConflicts; } diff --git a/x-pack/plugins/telemetry_collection_xpack/server/telemetry_collection/get_license.ts b/x-pack/plugins/telemetry_collection_xpack/server/telemetry_collection/get_license.ts index 64d9aee7b0ac7d..3db9f06bd8bf6a 100644 --- a/x-pack/plugins/telemetry_collection_xpack/server/telemetry_collection/get_license.ts +++ b/x-pack/plugins/telemetry_collection_xpack/server/telemetry_collection/get_license.ts @@ -4,30 +4,15 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import type { estypes } from '@elastic/elasticsearch'; import { ElasticsearchClient } from 'src/core/server'; -// From https://www.elastic.co/guide/en/elasticsearch/reference/current/get-license.html -export interface ESLicense { - status: string; - uid: string; - hkey: string; - type: string; - issue_date: string; - issue_date_in_millis: number; - expiry_date: string; - expiry_date_in_millis: number; - max_nodes: number; - issued_to: string; - issuer: string; - start_date_in_millis: number; - max_resource_units: number; -} +export type ESLicense = estypes.LicenseInformation; let cachedLicense: ESLicense | undefined; async function fetchLicense(esClient: ElasticsearchClient, local: boolean) { - const { body } = await esClient.license.get<{ license: ESLicense }>({ + const { body } = await esClient.license.get({ local, // For versions >= 7.6 and < 8.0, this flag is needed otherwise 'platinum' is returned for 'enterprise' license. accept_enterprise: true, diff --git a/x-pack/plugins/telemetry_collection_xpack/server/telemetry_collection/get_stats_with_xpack.test.ts b/x-pack/plugins/telemetry_collection_xpack/server/telemetry_collection/get_stats_with_xpack.test.ts index 12f2f24502ce0b..6ddb10e825684e 100644 --- a/x-pack/plugins/telemetry_collection_xpack/server/telemetry_collection/get_stats_with_xpack.test.ts +++ b/x-pack/plugins/telemetry_collection_xpack/server/telemetry_collection/get_stats_with_xpack.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import type { estypes } from '@elastic/elasticsearch'; import { coreMock, elasticsearchServiceMock } from '../../../../../src/core/server/mocks'; import { getStatsWithXpack } from './get_stats_with_xpack'; @@ -81,8 +82,8 @@ function mockEsClient() { body: { cluster_uuid: 'test', cluster_name: 'test', - version: { number: '8.0.0' }, - }, + version: { number: '8.0.0' } as estypes.ElasticsearchVersionInfo, + } as estypes.RootNodeInfoResponse, } ); diff --git a/x-pack/plugins/transform/server/routes/api/field_histograms.ts b/x-pack/plugins/transform/server/routes/api/field_histograms.ts index bfe2f470785690..bb2a1b278a5c20 100644 --- a/x-pack/plugins/transform/server/routes/api/field_histograms.ts +++ b/x-pack/plugins/transform/server/routes/api/field_histograms.ts @@ -41,6 +41,7 @@ export function registerFieldHistogramsRoutes({ router, license }: RouteDependen query, fields, samplerShardSize, + // @ts-expect-error script is not compatible with StoredScript from @elastic/elasticsearch: string is not supported runtimeMappings ); diff --git a/x-pack/plugins/transform/server/routes/api/privileges.ts b/x-pack/plugins/transform/server/routes/api/privileges.ts index d54a62b27f2629..f1900079ec9c49 100644 --- a/x-pack/plugins/transform/server/routes/api/privileges.ts +++ b/x-pack/plugins/transform/server/routes/api/privileges.ts @@ -32,7 +32,6 @@ export function registerPrivilegesRoute({ router, license }: RouteDependencies) const { body: { has_all_requested: hasAllPrivileges, cluster }, } = await ctx.core.elasticsearch.client.asCurrentUser.security.hasPrivileges({ - method: 'POST', body: { cluster: APP_CLUSTER_PRIVILEGES, }, @@ -45,9 +44,7 @@ export function registerPrivilegesRoute({ router, license }: RouteDependencies) // Get all index privileges the user has const { body: { indices }, - } = await ctx.core.elasticsearch.client.asCurrentUser.security.getUserPrivileges({ - method: 'GET', - }); + } = await ctx.core.elasticsearch.client.asCurrentUser.security.getUserPrivileges(); // Check if they have all the required index privileges for at least one index const oneIndexWithAllPrivileges = indices.find(({ privileges }: { privileges: string[] }) => { diff --git a/x-pack/plugins/transform/server/routes/api/transforms.ts b/x-pack/plugins/transform/server/routes/api/transforms.ts index 93f5caf7cf5b09..1dd136cb484fa4 100644 --- a/x-pack/plugins/transform/server/routes/api/transforms.ts +++ b/x-pack/plugins/transform/server/routes/api/transforms.ts @@ -206,6 +206,7 @@ export function registerTransformsRoutes(routeDependencies: RouteDependencies) { await ctx.core.elasticsearch.client.asCurrentUser.transform .putTransform({ + // @ts-expect-error @elastic/elasticsearch max_page_search_size is required in TransformPivot body: req.body, transform_id: transformId, }) @@ -250,6 +251,7 @@ export function registerTransformsRoutes(routeDependencies: RouteDependencies) { const { body, } = await ctx.core.elasticsearch.client.asCurrentUser.transform.updateTransform({ + // @ts-expect-error query doesn't satisfy QueryContainer from @elastic/elasticsearch body: req.body, transform_id: transformId, }); @@ -452,9 +454,12 @@ async function deleteTransforms( transform_id: transformId, }); const transformConfig = body.transforms[0]; + // @ts-expect-error @elastic/elasticsearch doesn't provide typings for Transform destinationIndex = Array.isArray(transformConfig.dest.index) - ? transformConfig.dest.index[0] - : transformConfig.dest.index; + ? // @ts-expect-error @elastic/elasticsearch doesn't provide typings for Transform + transformConfig.dest.index[0] + : // @ts-expect-error @elastic/elasticsearch doesn't provide typings for Transform + transformConfig.dest.index; } catch (getTransformConfigError) { transformDeleted.error = getTransformConfigError.meta.body.error; results[transformId] = { @@ -539,6 +544,7 @@ const previewTransformHandler: RequestHandler< try { const reqBody = req.body; const { body } = await ctx.core.elasticsearch.client.asCurrentUser.transform.previewTransform({ + // @ts-expect-error max_page_search_size is required in TransformPivot body: reqBody, }); if (isLatestTransform(reqBody)) { diff --git a/x-pack/plugins/transform/server/routes/api/transforms_audit_messages.ts b/x-pack/plugins/transform/server/routes/api/transforms_audit_messages.ts index 4a6b2c674e4297..b762f20b55b481 100644 --- a/x-pack/plugins/transform/server/routes/api/transforms_audit_messages.ts +++ b/x-pack/plugins/transform/server/routes/api/transforms_audit_messages.ts @@ -6,7 +6,7 @@ */ import { transformIdParamSchema, TransformIdParamSchema } from '../../../common/api_schemas/common'; -import { AuditMessage, TransformMessage } from '../../../common/types/messages'; +import { AuditMessage } from '../../../common/types/messages'; import { RouteDependencies } from '../../types'; @@ -78,19 +78,28 @@ export function registerTransformsAuditMessagesRoutes({ router, license }: Route } try { - const { body: resp } = await ctx.core.elasticsearch.client.asCurrentUser.search({ + const { + body: resp, + } = await ctx.core.elasticsearch.client.asCurrentUser.search({ index: ML_DF_NOTIFICATION_INDEX_PATTERN, ignore_unavailable: true, size: SIZE, body: { - sort: [{ timestamp: { order: 'desc' } }, { transform_id: { order: 'asc' } }], + sort: [ + { timestamp: { order: 'desc' as const } }, + { transform_id: { order: 'asc' as const } }, + ], query, }, }); - let messages: TransformMessage[] = []; - if (resp.hits.total.value > 0) { - messages = resp.hits.hits.map((hit: AuditMessage) => hit._source); + let messages: AuditMessage[] = []; + // TODO: remove typeof checks when appropriate overloading is added for the `search` API + if ( + (typeof resp.hits.total === 'number' && resp.hits.total > 0) || + (typeof resp.hits.total === 'object' && resp.hits.total.value > 0) + ) { + messages = resp.hits.hits.map((hit) => hit._source!); messages.reverse(); } return res.ok({ body: messages }); diff --git a/x-pack/plugins/triggers_actions_ui/server/data/lib/time_series_query.ts b/x-pack/plugins/triggers_actions_ui/server/data/lib/time_series_query.ts index 78462d9969929a..98212f1dc6aaf6 100644 --- a/x-pack/plugins/triggers_actions_ui/server/data/lib/time_series_query.ts +++ b/x-pack/plugins/triggers_actions_ui/server/data/lib/time_series_query.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { Logger, ElasticsearchClient } from 'kibana/server'; import { DEFAULT_GROUPS } from '../index'; import { getDateRangeInfo } from './date_range_info'; @@ -127,14 +127,14 @@ export async function timeSeriesQuery( const logPrefix = 'indexThreshold timeSeriesQuery: callCluster'; logger.debug(`${logPrefix} call: ${JSON.stringify(esQuery)}`); - let esResult: SearchResponse; + let esResult: estypes.SearchResponse; // note there are some commented out console.log()'s below, which are left // in, as they are VERY useful when debugging these queries; debug logging // isn't as nice since it's a single long JSON line. // console.log('time_series_query.ts request\n', JSON.stringify(esQuery, null, 4)); try { - esResult = (await esClient.search>(esQuery, { ignore: [404] })).body; + esResult = (await esClient.search(esQuery, { ignore: [404] })).body; } catch (err) { // console.log('time_series_query.ts error\n', JSON.stringify(err, null, 4)); logger.warn(`${logPrefix} error: ${err.message}`); @@ -149,7 +149,7 @@ export async function timeSeriesQuery( function getResultFromEs( isCountAgg: boolean, isGroupAgg: boolean, - esResult: SearchResponse + esResult: estypes.SearchResponse ): TimeSeriesResult { const aggregations = esResult?.aggregations || {}; @@ -164,6 +164,7 @@ function getResultFromEs( delete aggregations.dateAgg; } + // @ts-expect-error @elastic/elasticsearch Aggregate does not specify buckets const groupBuckets = aggregations.groupAgg?.buckets || []; const result: TimeSeriesResult = { results: [], diff --git a/x-pack/plugins/triggers_actions_ui/server/data/routes/indices.ts b/x-pack/plugins/triggers_actions_ui/server/data/routes/indices.ts index c029f5b8bdaed0..3245a9b8a983f7 100644 --- a/x-pack/plugins/triggers_actions_ui/server/data/routes/indices.ts +++ b/x-pack/plugins/triggers_actions_ui/server/data/routes/indices.ts @@ -105,7 +105,9 @@ async function getIndicesFromPattern( return []; } - return (response.aggregations as IndiciesAggregation).indices.buckets.map((bucket) => bucket.key); + return ((response.aggregations as unknown) as IndiciesAggregation).indices.buckets.map( + (bucket) => bucket.key + ); } async function getAliasesFromPattern( @@ -118,14 +120,15 @@ async function getAliasesFromPattern( }; const result: string[] = []; - const { body: response } = await esClient.indices.getAlias(params); + const response = await esClient.indices.getAlias(params); + const responseBody = response.body; - if (response.status === 404) { + if (response.statusCode === 404) { return result; } - for (const index of Object.keys(response)) { - const aliasRecord = response[index]; + for (const index of Object.keys(responseBody)) { + const aliasRecord = responseBody[index]; if (aliasRecord.aliases) { const aliases = Object.keys(aliasRecord.aliases); result.push(...aliases); diff --git a/x-pack/plugins/upgrade_assistant/server/lib/es_indices_state_check.ts b/x-pack/plugins/upgrade_assistant/server/lib/es_indices_state_check.ts index d352ab77b56f36..d2595916496a5b 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/es_indices_state_check.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/es_indices_state_check.ts @@ -15,7 +15,7 @@ export const esIndicesStateCheck = async ( asCurrentUser: ElasticsearchClient, indices: string[] ): Promise => { - const { body: response } = await asCurrentUser.indices.resolveIndex({ + const { body: response } = await asCurrentUser.indices.resolveIndex({ name: '*', expand_wildcards: 'all', }); @@ -23,7 +23,7 @@ export const esIndicesStateCheck = async ( const result: StatusCheckResult = {}; indices.forEach((index) => { - result[index] = getIndexState(index, response); + result[index] = getIndexState(index, response as ResolveIndexResponseFromES); }); return result; diff --git a/x-pack/plugins/upgrade_assistant/server/lib/es_migration_apis.test.ts b/x-pack/plugins/upgrade_assistant/server/lib/es_migration_apis.test.ts index 9ab8d0aa7cffb6..2620fe31d62777 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/es_migration_apis.test.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/es_migration_apis.test.ts @@ -31,6 +31,7 @@ describe('getUpgradeAssistantStatus', () => { const esClient = elasticsearchServiceMock.createScopedClusterClient(); esClient.asCurrentUser.migration.deprecations.mockResolvedValue( + // @ts-expect-error not full interface asApiResponse(deprecationsResponse) ); @@ -48,6 +49,7 @@ describe('getUpgradeAssistantStatus', () => { it('returns readyForUpgrade === false when critical issues found', async () => { esClient.asCurrentUser.migration.deprecations.mockResolvedValue( + // @ts-expect-error not full interface asApiResponse({ cluster_settings: [{ level: 'critical', message: 'Do count me', url: 'https://...' }], node_settings: [], @@ -64,6 +66,7 @@ describe('getUpgradeAssistantStatus', () => { it('returns readyForUpgrade === true when no critical issues found', async () => { esClient.asCurrentUser.migration.deprecations.mockResolvedValue( + // @ts-expect-error not full interface asApiResponse({ cluster_settings: [{ level: 'warning', message: 'Do not count me', url: 'https://...' }], node_settings: [], @@ -80,6 +83,7 @@ describe('getUpgradeAssistantStatus', () => { it('filters out security realm deprecation on Cloud', async () => { esClient.asCurrentUser.migration.deprecations.mockResolvedValue( + // @ts-expect-error not full interface asApiResponse({ cluster_settings: [ { diff --git a/x-pack/plugins/upgrade_assistant/server/lib/es_migration_apis.ts b/x-pack/plugins/upgrade_assistant/server/lib/es_migration_apis.ts index 3486603341674d..e775190d426dfd 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/es_migration_apis.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/es_migration_apis.ts @@ -19,9 +19,7 @@ export async function getUpgradeAssistantStatus( dataClient: IScopedClusterClient, isCloudEnabled: boolean ): Promise { - const { - body: deprecations, - } = await dataClient.asCurrentUser.migration.deprecations(); + const { body: deprecations } = await dataClient.asCurrentUser.migration.deprecations(); const cluster = getClusterDeprecations(deprecations, isCloudEnabled); const indices = getCombinedIndexInfos(deprecations); diff --git a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_actions.test.ts b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_actions.test.ts index 592c2d15b9c0c5..cffd49e5bd38a4 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_actions.test.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_actions.test.ts @@ -283,6 +283,7 @@ describe('ReindexActions', () => { it('returns flat settings', async () => { clusterClient.asCurrentUser.indices.get.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ myIndex: { settings: { 'index.mySetting': '1' }, diff --git a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts index a91cf8ddeada9d..ae0a6f97e29d55 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.test.ts @@ -105,6 +105,7 @@ describe('reindexService', () => { it('calls security API with basic requirements', async () => { clusterClient.asCurrentUser.security.hasPrivileges.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ has_all_requested: true }) ); @@ -130,6 +131,7 @@ describe('reindexService', () => { it('includes manage_ml for ML indices', async () => { clusterClient.asCurrentUser.security.hasPrivileges.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ has_all_requested: true }) ); @@ -154,6 +156,7 @@ describe('reindexService', () => { it('includes checking for permissions on the baseName which could be an alias', async () => { clusterClient.asCurrentUser.security.hasPrivileges.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ has_all_requested: true }) ); @@ -183,6 +186,7 @@ describe('reindexService', () => { it('includes manage_watcher for watcher indices', async () => { clusterClient.asCurrentUser.security.hasPrivileges.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ has_all_requested: true, }) @@ -440,6 +444,7 @@ describe('reindexService', () => { }, } as any); + // @ts-expect-error not full interface clusterClient.asCurrentUser.tasks.cancel.mockResolvedValueOnce(asApiResponse(true)); await service.cancelReindexing('myIndex'); @@ -564,6 +569,7 @@ describe('reindexService', () => { f() ); clusterClient.asCurrentUser.nodes.info.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ nodes: { nodeX: { version: '6.7.0-alpha' } } }) ); clusterClient.asCurrentUser.ml.setUpgradeMode.mockResolvedValueOnce( @@ -596,6 +602,7 @@ describe('reindexService', () => { ); clusterClient.asCurrentUser.nodes.info.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ nodes: { nodeX: { version: '6.7.0-alpha' } } }) ); clusterClient.asCurrentUser.ml.setUpgradeMode.mockResolvedValueOnce( @@ -646,6 +653,7 @@ describe('reindexService', () => { f() ); clusterClient.asCurrentUser.nodes.info.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ nodes: { nodeX: { version: '6.7.0' } } }) ); clusterClient.asCurrentUser.ml.setUpgradeMode.mockResolvedValueOnce( @@ -670,6 +678,7 @@ describe('reindexService', () => { f() ); clusterClient.asCurrentUser.nodes.info.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ nodes: { nodeX: { version: '6.6.0' } } }) ); @@ -784,7 +793,7 @@ describe('reindexService', () => { expect(updatedOp.attributes.lastCompletedStep).toEqual(ReindexStep.readonly); expect(clusterClient.asCurrentUser.indices.putSettings).toHaveBeenCalledWith({ index: 'myIndex', - body: { 'index.blocks.write': true }, + body: { index: { blocks: { write: true } } }, }); }); @@ -823,6 +832,7 @@ describe('reindexService', () => { it('creates new index with settings and mappings and updates lastCompletedStep', async () => { actions.getFlatSettings.mockResolvedValueOnce(settingsMappings); clusterClient.asCurrentUser.indices.create.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ acknowledged: true }) ); const updatedOp = await service.processNextStep(reindexOp); @@ -839,10 +849,12 @@ describe('reindexService', () => { it('fails if create index is not acknowledged', async () => { clusterClient.asCurrentUser.indices.get.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ myIndex: settingsMappings }) ); clusterClient.asCurrentUser.indices.create.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ acknowledged: false }) ); const updatedOp = await service.processNextStep(reindexOp); @@ -854,6 +866,7 @@ describe('reindexService', () => { it('fails if create index fails', async () => { clusterClient.asCurrentUser.indices.get.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ myIndex: settingsMappings }) ); @@ -872,7 +885,7 @@ describe('reindexService', () => { // Original index should have been set back to allow reads. expect(clusterClient.asCurrentUser.indices.putSettings).toHaveBeenCalledWith({ index: 'myIndex', - body: { 'index.blocks.write': false }, + body: { index: { blocks: { write: false } } }, }); }); }); @@ -932,6 +945,7 @@ describe('reindexService', () => { describe('reindex task is not complete', () => { it('updates reindexTaskPercComplete', async () => { clusterClient.asCurrentUser.tasks.get.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ completed: false, task: { status: { created: 10, total: 100 } }, @@ -947,6 +961,7 @@ describe('reindexService', () => { describe('reindex task is complete', () => { it('deletes task, updates reindexTaskPercComplete, updates lastCompletedStep', async () => { clusterClient.asCurrentUser.tasks.get.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ completed: true, task: { status: { created: 100, total: 100 } }, @@ -954,12 +969,14 @@ describe('reindexService', () => { ); clusterClient.asCurrentUser.count.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ count: 100, }) ); clusterClient.asCurrentUser.delete.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ result: 'deleted', }) @@ -976,6 +993,7 @@ describe('reindexService', () => { it('fails if docs created is less than count in source index', async () => { clusterClient.asCurrentUser.tasks.get.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ completed: true, task: { status: { created: 95, total: 95 } }, @@ -983,6 +1001,7 @@ describe('reindexService', () => { ); clusterClient.asCurrentUser.count.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ count: 100, }) @@ -999,6 +1018,7 @@ describe('reindexService', () => { describe('reindex task is cancelled', () => { it('deletes task, updates status to cancelled', async () => { clusterClient.asCurrentUser.tasks.get.mockResolvedValueOnce( + // @ts-expect-error not full interface asApiResponse({ completed: true, task: { status: { created: 100, total: 100, canceled: 'by user request' } }, @@ -1006,6 +1026,7 @@ describe('reindexService', () => { ); clusterClient.asCurrentUser.delete.mockResolvedValue( + // @ts-expect-error not full interface asApiResponse({ result: 'deleted' }) ); diff --git a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts index 1b100b70f37503..d01275645965e2 100644 --- a/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts +++ b/x-pack/plugins/upgrade_assistant/server/lib/reindexing/reindex_service.ts @@ -226,7 +226,7 @@ export const reindexServiceFactory = ( if (reindexOp.attributes.lastCompletedStep >= ReindexStep.readonly) { await esClient.indices.putSettings({ index: reindexOp.attributes.indexName, - body: { 'index.blocks.write': false }, + body: { index: { blocks: { write: false } } }, }); } @@ -290,7 +290,7 @@ export const reindexServiceFactory = ( const { indexName } = reindexOp.attributes; const { body: putReadonly } = await esClient.indices.putSettings({ index: indexName, - body: { 'index.blocks.write': true }, + body: { index: { blocks: { write: true } } }, }); if (!putReadonly.acknowledged) { @@ -363,7 +363,10 @@ export const reindexServiceFactory = ( return actions.updateReindexOp(reindexOp, { lastCompletedStep: ReindexStep.reindexStarted, - reindexTaskId: startReindexResponse.task, + reindexTaskId: + startReindexResponse.task === undefined + ? startReindexResponse.task + : String(startReindexResponse.task), reindexTaskPercComplete: 0, reindexOptions: { ...(reindexOptions ?? {}), @@ -389,11 +392,11 @@ export const reindexServiceFactory = ( if (!taskResponse.completed) { // Updated the percent complete - const perc = taskResponse.task.status.created / taskResponse.task.status.total; + const perc = taskResponse.task.status!.created / taskResponse.task.status!.total; return actions.updateReindexOp(reindexOp, { reindexTaskPercComplete: perc, }); - } else if (taskResponse.task.status.canceled === 'by user request') { + } else if (taskResponse.task.status?.canceled === 'by user request') { // Set the status to cancelled reindexOp = await actions.updateReindexOp(reindexOp, { status: ReindexStatus.cancelled, @@ -403,9 +406,11 @@ export const reindexServiceFactory = ( reindexOp = await cleanupChanges(reindexOp); } else { // Check that it reindexed all documents - const { body: count } = await esClient.count({ index: reindexOp.attributes.indexName }); + const { + body: { count }, + } = await esClient.count({ index: reindexOp.attributes.indexName }); - if (taskResponse.task.status.created < count) { + if (taskResponse.task.status!.created < count) { // Include the entire task result in the error message. This should be guaranteed // to be JSON-serializable since it just came back from Elasticsearch. throw error.reindexTaskFailed(`Reindexing failed: ${JSON.stringify(taskResponse)}`); diff --git a/x-pack/plugins/uptime/common/utils/as_mutable_array.ts b/x-pack/plugins/uptime/common/utils/as_mutable_array.ts new file mode 100644 index 00000000000000..ce1d7e607ec4c5 --- /dev/null +++ b/x-pack/plugins/uptime/common/utils/as_mutable_array.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. + */ + +// Sometimes we use `as const` to have a more specific type, +// because TypeScript by default will widen the value type of an +// array literal. Consider the following example: +// +// const filter = [ +// { term: { 'agent.name': 'nodejs' } }, +// { range: { '@timestamp': { gte: 'now-15m ' }} +// ]; + +// The result value type will be: + +// const filter: ({ +// term: { +// 'agent.name'?: string +// }; +// range?: undefined +// } | { +// term?: undefined; +// range: { +// '@timestamp': { +// gte: string +// } +// } +// })[]; + +// This can sometimes leads to issues. In those cases, we can +// use `as const`. However, the Readonly type is not compatible +// with Array. This function returns a mutable version of a type. + +export function asMutableArray>( + arr: T +): T extends Readonly<[...infer U]> ? U : unknown[] { + return arr as any; +} diff --git a/x-pack/plugins/uptime/server/lib/lib.ts b/x-pack/plugins/uptime/server/lib/lib.ts index 1a7cef504b0190..12e58b94cfa500 100644 --- a/x-pack/plugins/uptime/server/lib/lib.ts +++ b/x-pack/plugins/uptime/server/lib/lib.ts @@ -16,7 +16,7 @@ import { UMBackendFrameworkAdapter } from './adapters'; import { UMLicenseCheck } from './domains'; import { UptimeRequests } from './requests'; import { savedObjectsAdapter } from './saved_objects'; -import { ESSearchResponse } from '../../../../../typings/elasticsearch'; +import { ESSearchRequest, ESSearchResponse } from '../../../../../typings/elasticsearch'; export interface UMDomainLibs { requests: UptimeRequests; @@ -54,7 +54,9 @@ export function createUptimeESClient({ return { baseESClient: esClient, - async search(params: TParams): Promise<{ body: ESSearchResponse }> { + async search( + params: TParams + ): Promise<{ body: ESSearchResponse }> { let res: any; let esError: any; const dynamicSettings = await savedObjectsAdapter.getUptimeDynamicSettings( diff --git a/x-pack/plugins/uptime/server/lib/requests/get_certs.ts b/x-pack/plugins/uptime/server/lib/requests/get_certs.ts index 2824fa9af35ad4..1b20ed9085fefc 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_certs.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_certs.ts @@ -48,7 +48,7 @@ export const getCerts: UMElasticsearchQueryFn = asyn { multi_match: { query: escape(search), - type: 'phrase_prefix', + type: 'phrase_prefix' as const, fields: [ 'monitor.id.text', 'monitor.name.text', @@ -98,7 +98,7 @@ export const getCerts: UMElasticsearchQueryFn = asyn field: 'monitor.id', }, name: 'monitors', - sort: [{ 'monitor.id': 'asc' }], + sort: [{ 'monitor.id': 'asc' as const }], }, }, aggs: { @@ -154,7 +154,7 @@ export const getCerts: UMElasticsearchQueryFn = asyn const sha1 = server?.hash?.sha1; const sha256 = server?.hash?.sha256; - const monitors = hit.inner_hits.monitors.hits.hits.map((monitor: any) => ({ + const monitors = hit.inner_hits!.monitors.hits.hits.map((monitor: any) => ({ name: monitor._source?.monitor.name, id: monitor._source?.monitor.id, url: monitor._source?.url?.full, diff --git a/x-pack/plugins/uptime/server/lib/requests/get_journey_details.ts b/x-pack/plugins/uptime/server/lib/requests/get_journey_details.ts index de37688b155f5a..560653950c6f88 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_journey_details.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_journey_details.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { QueryContainer } from '@elastic/elasticsearch/api/types'; import { UMElasticsearchQueryFn } from '../adapters/framework'; import { SyntheticsJourneyApiResponse } from '../../../common/runtime_types'; @@ -30,7 +31,7 @@ export const getJourneyDetails: UMElasticsearchQueryFn< 'synthetics.type': 'journey/start', }, }, - ], + ] as QueryContainer[], }, }, size: 1, @@ -55,7 +56,7 @@ export const getJourneyDetails: UMElasticsearchQueryFn< 'synthetics.type': 'journey/start', }, }, - ], + ] as QueryContainer[], }, }, _source: ['@timestamp', 'monitor.check_group'], @@ -78,7 +79,7 @@ export const getJourneyDetails: UMElasticsearchQueryFn< ], }, }, - sort: [{ '@timestamp': { order: 'desc' } }], + sort: [{ '@timestamp': { order: 'desc' as const } }], }; const nextParams = { @@ -97,7 +98,7 @@ export const getJourneyDetails: UMElasticsearchQueryFn< ], }, }, - sort: [{ '@timestamp': { order: 'asc' } }], + sort: [{ '@timestamp': { order: 'asc' as const } }], }; const { body: previousJourneyResult } = await uptimeEsClient.search({ body: previousParams }); diff --git a/x-pack/plugins/uptime/server/lib/requests/get_journey_failed_steps.ts b/x-pack/plugins/uptime/server/lib/requests/get_journey_failed_steps.ts index 9865bd95fe9613..5c4e263468947b 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_journey_failed_steps.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_journey_failed_steps.ts @@ -5,6 +5,9 @@ * 2.0. */ +import { QueryContainer } from '@elastic/elasticsearch/api/types'; +import { SearchHit } from '../../../../../../typings/elasticsearch'; +import { asMutableArray } from '../../../common/utils/as_mutable_array'; import { UMElasticsearchQueryFn } from '../adapters/framework'; import { Ping } from '../../../common/runtime_types'; @@ -35,10 +38,13 @@ export const getJourneyFailedSteps: UMElasticsearchQueryFn { + return ((result.hits.hits as Array>).map((h) => { const source = h._source as Ping & { '@timestamp': string }; return { ...source, diff --git a/x-pack/plugins/uptime/server/lib/requests/get_journey_screenshot.ts b/x-pack/plugins/uptime/server/lib/requests/get_journey_screenshot.ts index faa260eb9abd47..6a533d558c7211 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_journey_screenshot.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_journey_screenshot.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { QueryContainer } from '@elastic/elasticsearch/api/types'; import { UMElasticsearchQueryFn } from '../adapters/framework'; import { Ping } from '../../../common/runtime_types/ping'; @@ -33,7 +34,7 @@ export const getJourneyScreenshot: UMElasticsearchQueryFn< 'synthetics.type': 'step/screenshot', }, }, - ], + ] as QueryContainer[], }, }, aggs: { diff --git a/x-pack/plugins/uptime/server/lib/requests/get_journey_steps.ts b/x-pack/plugins/uptime/server/lib/requests/get_journey_steps.ts index 43d17cb9381597..bfb8f4d881c3d8 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_journey_steps.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_journey_steps.ts @@ -5,6 +5,9 @@ * 2.0. */ +import { QueryContainer } from '@elastic/elasticsearch/api/types'; +import { SearchHit } from 'typings/elasticsearch/search'; +import { asMutableArray } from '../../../common/utils/as_mutable_array'; import { UMElasticsearchQueryFn } from '../adapters/framework'; import { Ping } from '../../../common/runtime_types'; @@ -42,10 +45,13 @@ export const getJourneySteps: UMElasticsearchQueryFn (h?._source as Ping).synthetics?.type === 'step/screenshot') - .map((h) => (h?._source as Ping).synthetics?.step?.index as number); + const screenshotIndexes: number[] = (result.hits.hits as Array>) + .filter((h) => h._source?.synthetics?.type === 'step/screenshot') + .map((h) => h._source?.synthetics?.step?.index as number); - return (result.hits.hits - .filter((h) => (h?._source as Ping).synthetics?.type !== 'step/screenshot') + return ((result.hits.hits as Array>) + .filter((h) => h._source?.synthetics?.type !== 'step/screenshot') .map((h) => { const source = h._source as Ping & { '@timestamp': string }; return { diff --git a/x-pack/plugins/uptime/server/lib/requests/get_last_successful_step.ts b/x-pack/plugins/uptime/server/lib/requests/get_last_successful_step.ts index 82958167341c09..6f88e7e37e55e6 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_last_successful_step.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_last_successful_step.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; import { UMElasticsearchQueryFn } from '../adapters/framework'; import { Ping } from '../../../common/runtime_types/ping'; @@ -18,7 +19,7 @@ export const getStepLastSuccessfulStep: UMElasticsearchQueryFn< GetStepScreenshotParams, any > = async ({ uptimeEsClient, monitorId, stepIndex, timestamp }) => { - const lastSuccessCheckParams = { + const lastSuccessCheckParams: estypes.SearchRequest['body'] = { size: 1, sort: [ { diff --git a/x-pack/plugins/uptime/server/lib/requests/get_latest_monitor.ts b/x-pack/plugins/uptime/server/lib/requests/get_latest_monitor.ts index 14190413ac7e7e..ad8b345d7b276d 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_latest_monitor.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_latest_monitor.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { QueryContainer } from '@elastic/elasticsearch/api/types'; import { UMElasticsearchQueryFn } from '../adapters'; import { Ping } from '../../../common/runtime_types'; @@ -44,20 +45,20 @@ export const getLatestMonitor: UMElasticsearchQueryFn = async ({ uptimeEsClient, monitorId, dateStart, dateEnd }) => { - const sortOptions: SortOptions = [ + const sortOptions = [ { '@timestamp': { - order: 'desc', + order: 'desc' as const, }, }, ]; diff --git a/x-pack/plugins/uptime/server/lib/requests/get_monitor_states.ts b/x-pack/plugins/uptime/server/lib/requests/get_monitor_states.ts index ef90794e634b77..16aca6f8238fbe 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_monitor_states.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_monitor_states.ts @@ -152,7 +152,7 @@ export const getHistogramForMonitors = async ( }; const { body: result } = await queryContext.search({ body: params }); - const histoBuckets: any[] = result.aggregations?.histogram.buckets ?? []; + const histoBuckets: any[] = (result.aggregations as any)?.histogram.buckets ?? []; const simplified = histoBuckets.map((histoBucket: any): { timestamp: number; byId: any } => { const byId: { [key: string]: number } = {}; histoBucket.by_id.buckets.forEach((idBucket: any) => { diff --git a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts index b588127b064830..8b46070f701f2d 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts @@ -6,6 +6,8 @@ */ import { JsonObject } from 'src/plugins/kibana_utils/public'; +import { QueryContainer } from '@elastic/elasticsearch/api/types'; +import { asMutableArray } from '../../../common/utils/as_mutable_array'; import { UMElasticsearchQueryFn } from '../adapters'; import { Ping } from '../../../common/runtime_types/ping'; @@ -68,7 +70,7 @@ export const getMonitorStatus: UMElasticsearchQueryFn< }, // append user filters, if defined ...(filters?.bool ? [filters] : []), - ], + ] as QueryContainer[], }, }, size: 0, @@ -81,7 +83,7 @@ export const getMonitorStatus: UMElasticsearchQueryFn< * to tell Elasticsearch where it should start on subsequent queries. */ ...(afterKey ? { after: afterKey } : {}), - sources: [ + sources: asMutableArray([ { monitorId: { terms: { @@ -104,7 +106,7 @@ export const getMonitorStatus: UMElasticsearchQueryFn< }, }, }, - ], + ] as const), }, aggs: { fields: { diff --git a/x-pack/plugins/uptime/server/lib/requests/get_network_events.ts b/x-pack/plugins/uptime/server/lib/requests/get_network_events.ts index 970af80576cad8..246b2001a93810 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_network_events.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_network_events.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { QueryContainer } from '@elastic/elasticsearch/api/types'; import { UMElasticsearchQueryFn } from '../adapters/framework'; import { NetworkEvent } from '../../../common/runtime_types'; @@ -29,7 +30,7 @@ export const getNetworkEvents: UMElasticsearchQueryFn< { term: { 'synthetics.type': 'journey/network_info' } }, { term: { 'monitor.check_group': checkGroup } }, { term: { 'synthetics.step.index': Number(stepIndex) } }, - ], + ] as QueryContainer[], }, }, // NOTE: This limit may need tweaking in the future. Users can technically perform multiple diff --git a/x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.ts b/x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.ts index d59da8029f1b95..0362ac30ac7132 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.ts @@ -53,7 +53,7 @@ export const getPingHistogram: UMElasticsearchQueryFn< { multi_match: { query: escape(query), - type: 'phrase_prefix', + type: 'phrase_prefix' as const, fields: ['monitor.id.text', 'monitor.name.text', 'url.full.text'], }, }, @@ -68,7 +68,7 @@ export const getPingHistogram: UMElasticsearchQueryFn< date_histogram: { field: '@timestamp', fixed_interval: bucketSize || minInterval + 'ms', - missing: 0, + missing: '0', }, aggs: { down: { diff --git a/x-pack/plugins/uptime/server/lib/requests/get_pings.ts b/x-pack/plugins/uptime/server/lib/requests/get_pings.ts index 453663ba737fbd..cf7a85e60f753a 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_pings.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_pings.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { QueryContainer } from '@elastic/elasticsearch/api/types'; import { UMElasticsearchQueryFn } from '../adapters/framework'; import { GetPingsParams, @@ -73,13 +74,13 @@ export const getPings: UMElasticsearchQueryFn = a { range: { '@timestamp': { gte: from, lte: to } } }, ...(monitorId ? [{ term: { 'monitor.id': monitorId } }] : []), ...(status ? [{ term: { 'monitor.status': status } }] : []), - ], + ] as QueryContainer[], ...REMOVE_NON_SUMMARY_BROWSER_CHECKS, }, }, sort: [{ '@timestamp': { order: (sort ?? 'desc') as 'asc' | 'desc' } }], ...((locations ?? []).length > 0 - ? { post_filter: { terms: { 'observer.geo.name': locations } } } + ? { post_filter: { terms: { 'observer.geo.name': (locations as unknown) as string[] } } } : {}), }; diff --git a/x-pack/plugins/uptime/server/lib/requests/helper.ts b/x-pack/plugins/uptime/server/lib/requests/helper.ts index e3969f84c84852..c637c050946671 100644 --- a/x-pack/plugins/uptime/server/lib/requests/helper.ts +++ b/x-pack/plugins/uptime/server/lib/requests/helper.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { SearchResponse } from '@elastic/elasticsearch/api/types'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { ElasticsearchClientMock } from 'src/core/server/elasticsearch/client/mocks'; import { @@ -49,7 +50,7 @@ export const setupMockEsCompositeQuery = ( }, }; esMock.search.mockResolvedValueOnce({ - body: mockResponse, + body: (mockResponse as unknown) as SearchResponse, statusCode: 200, headers: {}, warnings: [], diff --git a/x-pack/plugins/uptime/server/lib/requests/search/find_potential_matches.ts b/x-pack/plugins/uptime/server/lib/requests/search/find_potential_matches.ts index 639a24a2bdffa4..179e9e809e59b9 100644 --- a/x-pack/plugins/uptime/server/lib/requests/search/find_potential_matches.ts +++ b/x-pack/plugins/uptime/server/lib/requests/search/find_potential_matches.ts @@ -22,7 +22,7 @@ export const findPotentialMatches = async ( const { body: queryResult } = await query(queryContext, searchAfter, size); const monitorIds: string[] = []; - (queryResult.aggregations?.monitors.buckets ?? []).forEach((b) => { + (queryResult.aggregations?.monitors.buckets ?? []).forEach((b: any) => { const monitorId = b.key.monitor_id; monitorIds.push(monitorId as string); }); @@ -40,7 +40,8 @@ const query = async (queryContext: QueryContext, searchAfter: any, size: number) body, }; - return await queryContext.search(params); + const response = await queryContext.search(params); + return response; }; const queryBody = async (queryContext: QueryContext, searchAfter: any, size: number) => { @@ -77,7 +78,9 @@ const queryBody = async (queryContext: QueryContext, searchAfter: any, size: num size, sources: [ { - monitor_id: { terms: { field: 'monitor.id', order: queryContext.cursorOrder() } }, + monitor_id: { + terms: { field: 'monitor.id' as const, order: queryContext.cursorOrder() }, + }, }, ], }, diff --git a/x-pack/test/accessibility/apps/index_lifecycle_management.ts b/x-pack/test/accessibility/apps/index_lifecycle_management.ts index d6ba222e50eb44..0305de740dc99a 100644 --- a/x-pack/test/accessibility/apps/index_lifecycle_management.ts +++ b/x-pack/test/accessibility/apps/index_lifecycle_management.ts @@ -55,6 +55,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { }); after(async () => { + // @ts-expect-error @elastic/elasticsearch DeleteSnapshotLifecycleRequest.policy_id is required await esClient.ilm.deleteLifecycle({ policy: TEST_POLICY_NAME }); }); diff --git a/x-pack/test/accessibility/apps/ml.ts b/x-pack/test/accessibility/apps/ml.ts index 164c7032d9dd36..41f8d5e56f8e6a 100644 --- a/x-pack/test/accessibility/apps/ml.ts +++ b/x-pack/test/accessibility/apps/ml.ts @@ -114,10 +114,11 @@ export default function ({ getService }: FtrProviderContext) { description: 'Test calendar', }); await ml.api.createCalendarEvents(calendarId, [ + // @ts-expect-error not full interface { description: eventDescription, - start_time: 1513641600000, - end_time: 1513728000000, + start_time: '1513641600000', + end_time: '1513728000000', }, ]); diff --git a/x-pack/test/api_integration/apis/es/has_privileges.ts b/x-pack/test/api_integration/apis/es/has_privileges.ts index 7403d27c4dd4bc..4cd1f70d4fffc0 100644 --- a/x-pack/test/api_integration/apis/es/has_privileges.ts +++ b/x-pack/test/api_integration/apis/es/has_privileges.ts @@ -32,6 +32,7 @@ export default function ({ getService }: FtrProviderContext) { name: 'hp_read_user', body: { cluster: [], + // @ts-expect-error unknown property index: [], applications: [ { diff --git a/x-pack/test/api_integration/apis/lens/telemetry.ts b/x-pack/test/api_integration/apis/lens/telemetry.ts index 1c321f37fa7feb..5d00a097d32a78 100644 --- a/x-pack/test/api_integration/apis/lens/telemetry.ts +++ b/x-pack/test/api_integration/apis/lens/telemetry.ts @@ -7,7 +7,6 @@ import moment from 'moment'; import expect from '@kbn/expect'; -import { Client } from '@elastic/elasticsearch'; import { FtrProviderContext } from '../../ftr_provider_context'; @@ -20,7 +19,7 @@ const COMMON_HEADERS = { export default ({ getService }: FtrProviderContext) => { const supertest = getService('supertest'); - const es: Client = getService('es'); + const es = getService('es'); async function assertExpectedSavedObjects(num: number) { // Make sure that new/deleted docs are available to search @@ -42,6 +41,7 @@ export default ({ getService }: FtrProviderContext) => { beforeEach(async () => { await es.deleteByQuery({ index: '.kibana', + // @ts-expect-error @elastic/elasticsearch DeleteByQueryRequest doesn't accept q parameter q: 'type:lens-ui-telemetry', wait_for_completion: true, refresh: true, @@ -52,6 +52,7 @@ export default ({ getService }: FtrProviderContext) => { afterEach(async () => { await es.deleteByQuery({ index: '.kibana', + // @ts-expect-error @elastic/elasticsearch DeleteByQueryRequest doesn't accept q parameter q: 'type:lens-ui-telemetry', wait_for_completion: true, refresh: true, diff --git a/x-pack/test/api_integration/apis/ml/annotations/create_annotations.ts b/x-pack/test/api_integration/apis/ml/annotations/create_annotations.ts index d1527db6b66ea3..21c1c1efbf9e8e 100644 --- a/x-pack/test/api_integration/apis/ml/annotations/create_annotations.ts +++ b/x-pack/test/api_integration/apis/ml/annotations/create_annotations.ts @@ -25,6 +25,7 @@ export default ({ getService }: FtrProviderContext) => { before(async () => { await esArchiver.loadIfNeeded('ml/farequote'); await ml.testResources.setKibanaTimeZoneToUTC(); + // @ts-expect-error not full interface await ml.api.createAnomalyDetectionJob(testJobConfig); }); diff --git a/x-pack/test/api_integration/apis/ml/annotations/delete_annotations.ts b/x-pack/test/api_integration/apis/ml/annotations/delete_annotations.ts index 422d00a21ce159..f6c4c98e1f7ea3 100644 --- a/x-pack/test/api_integration/apis/ml/annotations/delete_annotations.ts +++ b/x-pack/test/api_integration/apis/ml/annotations/delete_annotations.ts @@ -25,6 +25,7 @@ export default ({ getService }: FtrProviderContext) => { for (let i = 0; i < testSetupJobConfigs.length; i++) { const job = testSetupJobConfigs[i]; const annotationToIndex = testSetupAnnotations[i]; + // @ts-expect-error not full interface await ml.api.createAnomalyDetectionJob(job); await ml.api.indexAnnotation(annotationToIndex); } diff --git a/x-pack/test/api_integration/apis/ml/annotations/get_annotations.ts b/x-pack/test/api_integration/apis/ml/annotations/get_annotations.ts index ac8438170c8824..c0e81955ad5957 100644 --- a/x-pack/test/api_integration/apis/ml/annotations/get_annotations.ts +++ b/x-pack/test/api_integration/apis/ml/annotations/get_annotations.ts @@ -26,6 +26,7 @@ export default ({ getService }: FtrProviderContext) => { for (let i = 0; i < testSetupJobConfigs.length; i++) { const job = testSetupJobConfigs[i]; const annotationToIndex = testSetupAnnotations[i]; + // @ts-expect-error not full interface await ml.api.createAnomalyDetectionJob(job); await ml.api.indexAnnotation(annotationToIndex); } diff --git a/x-pack/test/api_integration/apis/ml/annotations/update_annotations.ts b/x-pack/test/api_integration/apis/ml/annotations/update_annotations.ts index 41406cb3443883..346f4562b77bb1 100644 --- a/x-pack/test/api_integration/apis/ml/annotations/update_annotations.ts +++ b/x-pack/test/api_integration/apis/ml/annotations/update_annotations.ts @@ -38,6 +38,7 @@ export default ({ getService }: FtrProviderContext) => { for (let i = 0; i < testSetupJobConfigs.length; i++) { const job = testSetupJobConfigs[i]; const annotationToIndex = testSetupAnnotations[i]; + // @ts-expect-error not full interface await ml.api.createAnomalyDetectionJob(job); await ml.api.indexAnnotation(annotationToIndex); } @@ -54,7 +55,7 @@ export default ({ getService }: FtrProviderContext) => { const originalAnnotation = annotationsForJob[0]; const annotationUpdateRequestBody = { ...commonAnnotationUpdateRequestBody, - job_id: originalAnnotation._source.job_id, + job_id: originalAnnotation._source?.job_id, _id: originalAnnotation._id, }; @@ -85,7 +86,7 @@ export default ({ getService }: FtrProviderContext) => { const originalAnnotation = annotationsForJob[0]; const annotationUpdateRequestBody = { ...commonAnnotationUpdateRequestBody, - job_id: originalAnnotation._source.job_id, + job_id: originalAnnotation._source?.job_id, _id: originalAnnotation._id, }; @@ -116,7 +117,7 @@ export default ({ getService }: FtrProviderContext) => { const annotationUpdateRequestBody = { ...commonAnnotationUpdateRequestBody, - job_id: originalAnnotation._source.job_id, + job_id: originalAnnotation._source?.job_id, _id: originalAnnotation._id, }; @@ -143,7 +144,7 @@ export default ({ getService }: FtrProviderContext) => { timestamp: Date.now(), end_timestamp: Date.now(), annotation: 'Updated annotation', - job_id: originalAnnotation._source.job_id, + job_id: originalAnnotation._source?.job_id, type: ANNOTATION_TYPE.ANNOTATION, event: 'model_change', detector_index: 2, diff --git a/x-pack/test/api_integration/apis/ml/anomaly_detectors/get.ts b/x-pack/test/api_integration/apis/ml/anomaly_detectors/get.ts index acbfcbe7acd475..03ee82eed27d02 100644 --- a/x-pack/test/api_integration/apis/ml/anomaly_detectors/get.ts +++ b/x-pack/test/api_integration/apis/ml/anomaly_detectors/get.ts @@ -52,6 +52,7 @@ export default ({ getService }: FtrProviderContext) => { ]; for (const jobConfig of mockJobConfigs) { + // @ts-expect-error not full interface await ml.api.createAnomalyDetectionJob(jobConfig); } } diff --git a/x-pack/test/api_integration/apis/ml/calendars/create_calendars.ts b/x-pack/test/api_integration/apis/ml/calendars/create_calendars.ts index fac62237aa74e5..01b87e6059a29d 100644 --- a/x-pack/test/api_integration/apis/ml/calendars/create_calendars.ts +++ b/x-pack/test/api_integration/apis/ml/calendars/create_calendars.ts @@ -52,7 +52,11 @@ export default ({ getService }: FtrProviderContext) => { expect(createdCalendar.description).to.eql(requestBody.description); expect(createdCalendar.job_ids).to.eql(requestBody.job_ids); - await ml.api.waitForEventsToExistInCalendar(calendarId, requestBody.events); + await ml.api.waitForEventsToExistInCalendar( + calendarId, + // @ts-expect-error not full interface + requestBody.events + ); }); it('should not create new calendar for user without required permission', async () => { diff --git a/x-pack/test/api_integration/apis/ml/calendars/delete_calendars.ts b/x-pack/test/api_integration/apis/ml/calendars/delete_calendars.ts index a2e1709731aa74..dfbffad9dafdd0 100644 --- a/x-pack/test/api_integration/apis/ml/calendars/delete_calendars.ts +++ b/x-pack/test/api_integration/apis/ml/calendars/delete_calendars.ts @@ -34,6 +34,7 @@ export default ({ getService }: FtrProviderContext) => { beforeEach(async () => { await ml.api.createCalendar(calendarId, testCalendar); + // @ts-expect-error not full interface await ml.api.createCalendarEvents(calendarId, testEvents); }); diff --git a/x-pack/test/api_integration/apis/ml/calendars/get_calendars.ts b/x-pack/test/api_integration/apis/ml/calendars/get_calendars.ts index 243a40abe97a4a..175c678317e6c1 100644 --- a/x-pack/test/api_integration/apis/ml/calendars/get_calendars.ts +++ b/x-pack/test/api_integration/apis/ml/calendars/get_calendars.ts @@ -35,6 +35,7 @@ export default ({ getService }: FtrProviderContext) => { beforeEach(async () => { for (const testCalendar of testCalendars) { await ml.api.createCalendar(testCalendar.calendar_id, testCalendar); + // @ts-expect-error not full interface await ml.api.createCalendarEvents(testCalendar.calendar_id, testEvents); } }); @@ -54,6 +55,7 @@ export default ({ getService }: FtrProviderContext) => { expect(body).to.have.length(testCalendars.length); expect(body[0].events).to.have.length(testEvents.length); + // @ts-expect-error not full interface ml.api.assertAllEventsExistInCalendar(testEvents, body[0]); }); @@ -66,6 +68,7 @@ export default ({ getService }: FtrProviderContext) => { expect(body).to.have.length(testCalendars.length); expect(body[0].events).to.have.length(testEvents.length); + // @ts-expect-error not full interface ml.api.assertAllEventsExistInCalendar(testEvents, body[0]); }); @@ -89,6 +92,7 @@ export default ({ getService }: FtrProviderContext) => { beforeEach(async () => { await ml.api.createCalendar(calendarId, testCalendar); + // @ts-expect-error not full interface await ml.api.createCalendarEvents(calendarId, testEvents); }); @@ -106,6 +110,7 @@ export default ({ getService }: FtrProviderContext) => { expect(body.job_ids).to.eql(testCalendar.job_ids); expect(body.description).to.eql(testCalendar.description); expect(body.events).to.have.length(testEvents.length); + // @ts-expect-error not full interface ml.api.assertAllEventsExistInCalendar(testEvents, body); }); @@ -119,6 +124,7 @@ export default ({ getService }: FtrProviderContext) => { expect(body.job_ids).to.eql(testCalendar.job_ids); expect(body.description).to.eql(testCalendar.description); expect(body.events).to.have.length(testEvents.length); + // @ts-expect-error not full interface ml.api.assertAllEventsExistInCalendar(testEvents, body); }); diff --git a/x-pack/test/api_integration/apis/ml/calendars/helpers.ts b/x-pack/test/api_integration/apis/ml/calendars/helpers.ts index 7ddfc4b41679d8..f80c985c2676eb 100644 --- a/x-pack/test/api_integration/apis/ml/calendars/helpers.ts +++ b/x-pack/test/api_integration/apis/ml/calendars/helpers.ts @@ -5,13 +5,16 @@ * 2.0. */ -import { Calendar, CalendarEvent } from '../../../../../plugins/ml/server/models/calendar'; +import { estypes } from '@elastic/elasticsearch'; +import { Calendar } from '../../../../../plugins/ml/server/models/calendar'; + +type ScheduledEvent = estypes.ScheduledEvent; export const assertAllEventsExistInCalendar = ( - eventsToCheck: CalendarEvent[], + eventsToCheck: ScheduledEvent[], calendar: Calendar ): boolean => { - const updatedCalendarEvents = calendar.events as CalendarEvent[]; + const updatedCalendarEvents = calendar.events; let allEventsAreUpdated = true; for (const eventToCheck of eventsToCheck) { // if at least one of the events that we need to check is not in the updated events diff --git a/x-pack/test/api_integration/apis/ml/calendars/update_calendars.ts b/x-pack/test/api_integration/apis/ml/calendars/update_calendars.ts index 1ca9a663199436..4798a3407be115 100644 --- a/x-pack/test/api_integration/apis/ml/calendars/update_calendars.ts +++ b/x-pack/test/api_integration/apis/ml/calendars/update_calendars.ts @@ -42,6 +42,7 @@ export default ({ getService }: FtrProviderContext) => { beforeEach(async () => { await ml.api.createCalendar(calendarId, originalCalendar); + // @ts-expect-error not full interface await ml.api.createCalendarEvents(calendarId, originalEvents); }); @@ -70,6 +71,7 @@ export default ({ getService }: FtrProviderContext) => { expect(updatedEvents).to.have.length(updateCalendarRequestBody.events.length); await ml.api.waitForEventsToExistInCalendar( updatedCalendar.calendar_id, + // @ts-expect-error not full interface updateCalendarRequestBody.events ); }); diff --git a/x-pack/test/api_integration/apis/ml/jobs/common_jobs.ts b/x-pack/test/api_integration/apis/ml/jobs/common_jobs.ts index 5039ea550d47e6..2136e16e3c1534 100644 --- a/x-pack/test/api_integration/apis/ml/jobs/common_jobs.ts +++ b/x-pack/test/api_integration/apis/ml/jobs/common_jobs.ts @@ -7,6 +7,7 @@ import { Job, Datafeed } from '../../../../../plugins/ml/common/types/anomaly_detection_jobs'; +// @ts-expect-error not full interface export const SINGLE_METRIC_JOB_CONFIG: Job = { job_id: `jobs_summary_fq_single_${Date.now()}`, description: 'mean(responsetime) on farequote dataset with 15m bucket span', @@ -26,6 +27,7 @@ export const SINGLE_METRIC_JOB_CONFIG: Job = { model_plot_config: { enabled: true }, }; +// @ts-expect-error not full interface export const MULTI_METRIC_JOB_CONFIG: Job = { job_id: `jobs_summary_fq_multi_${Date.now()}`, description: 'mean(responsetime) partition=airline on farequote dataset with 1h bucket span', @@ -40,6 +42,7 @@ export const MULTI_METRIC_JOB_CONFIG: Job = { model_plot_config: { enabled: true }, }; +// @ts-expect-error not full interface export const DATAFEED_CONFIG: Datafeed = { datafeed_id: 'REPLACE', indices: ['ft_farequote'], diff --git a/x-pack/test/api_integration/apis/ml/results/get_anomalies_table_data.ts b/x-pack/test/api_integration/apis/ml/results/get_anomalies_table_data.ts index fbca2f2abcce39..1505e6fbe09bea 100644 --- a/x-pack/test/api_integration/apis/ml/results/get_anomalies_table_data.ts +++ b/x-pack/test/api_integration/apis/ml/results/get_anomalies_table_data.ts @@ -16,6 +16,7 @@ export default ({ getService }: FtrProviderContext) => { const supertest = getService('supertestWithoutAuth'); const ml = getService('ml'); + // @ts-expect-error not full interface const JOB_CONFIG: Job = { job_id: `fq_multi_1_ae`, description: @@ -35,6 +36,7 @@ export default ({ getService }: FtrProviderContext) => { model_plot_config: { enabled: true }, }; + // @ts-expect-error not full interface const DATAFEED_CONFIG: Datafeed = { datafeed_id: 'datafeed-fq_multi_1_se', indices: ['ft_farequote'], diff --git a/x-pack/test/api_integration/apis/ml/results/get_categorizer_stats.ts b/x-pack/test/api_integration/apis/ml/results/get_categorizer_stats.ts index 9952dca651ba09..ef677969d006f0 100644 --- a/x-pack/test/api_integration/apis/ml/results/get_categorizer_stats.ts +++ b/x-pack/test/api_integration/apis/ml/results/get_categorizer_stats.ts @@ -43,6 +43,7 @@ export default ({ getService }: FtrProviderContext) => { daily_model_snapshot_retention_after_days: 1, allow_lazy_open: false, }; + // @ts-expect-error not full interface const testDatafeedConfig: Datafeed = { datafeed_id: `datafeed-${jobId}`, indices: ['ft_module_sample_logs'], @@ -54,6 +55,7 @@ export default ({ getService }: FtrProviderContext) => { before(async () => { await esArchiver.loadIfNeeded('ml/module_sample_logs'); await ml.testResources.setKibanaTimeZoneToUTC(); + // @ts-expect-error not full interface await ml.api.createAndRunAnomalyDetectionLookbackJob(testJobConfig, testDatafeedConfig); }); diff --git a/x-pack/test/api_integration/apis/ml/results/get_stopped_partitions.ts b/x-pack/test/api_integration/apis/ml/results/get_stopped_partitions.ts index 7b03e0e3091dec..d00999b06b588c 100644 --- a/x-pack/test/api_integration/apis/ml/results/get_stopped_partitions.ts +++ b/x-pack/test/api_integration/apis/ml/results/get_stopped_partitions.ts @@ -40,6 +40,7 @@ export default ({ getService }: FtrProviderContext) => { daily_model_snapshot_retention_after_days: 1, allow_lazy_open: false, }; + // @ts-expect-error not full interface const datafeedConfig: Datafeed = { datafeed_id: `datafeed-${jobId}`, indices: ['ft_module_sample_logs'], @@ -50,6 +51,7 @@ export default ({ getService }: FtrProviderContext) => { return { testDescription: `stop_on_warn is ${stopOnWarn}`, jobId, + // @ts-expect-error not full interface jobConfig: { job_id: jobId, ...commonJobConfig, diff --git a/x-pack/test/api_integration/apis/security/roles.ts b/x-pack/test/api_integration/apis/security/roles.ts index 440bb4ca32f189..09b2d2eef9fbe4 100644 --- a/x-pack/test/api_integration/apis/security/roles.ts +++ b/x-pack/test/api_integration/apis/security/roles.ts @@ -312,6 +312,7 @@ export default function ({ getService }: FtrProviderContext) { metadata: { foo: 'test-metadata', }, + // @ts-expect-error @elastic/elasticsearch PutRoleRequest.body doesn't declare `transient_metadata` property transient_metadata: { enabled: true, }, diff --git a/x-pack/test/api_integration/apis/telemetry/telemetry_local.ts b/x-pack/test/api_integration/apis/telemetry/telemetry_local.ts index 7055c60b6cd348..a85e8ef82fc8c2 100644 --- a/x-pack/test/api_integration/apis/telemetry/telemetry_local.ts +++ b/x-pack/test/api_integration/apis/telemetry/telemetry_local.ts @@ -37,7 +37,7 @@ export default function ({ getService }: FtrProviderContext) { let stats: Record; before('disable monitoring and pull local stats', async () => { - await es.cluster.put_settings({ body: disableCollection }); + await es.cluster.putSettings({ body: disableCollection }); await new Promise((r) => setTimeout(r, 1000)); const { body } = await supertest diff --git a/x-pack/test/api_integration/apis/telemetry/telemetry_optin_notice_seen.ts b/x-pack/test/api_integration/apis/telemetry/telemetry_optin_notice_seen.ts index 749bea29613488..9e9211c4b58938 100644 --- a/x-pack/test/api_integration/apis/telemetry/telemetry_optin_notice_seen.ts +++ b/x-pack/test/api_integration/apis/telemetry/telemetry_optin_notice_seen.ts @@ -25,15 +25,13 @@ export default function optInTest({ getService }: FtrProviderContext) { await supertest.put('/api/telemetry/v2/userHasSeenNotice').set('kbn-xsrf', 'xxx').expect(200); const { - body: { - _source: { telemetry }, - }, - } = await client.get({ + body: { _source }, + } = await client.get<{ telemetry: { userHasSeenNotice: boolean } }>({ index: '.kibana', id: 'telemetry:telemetry', }); - expect(telemetry.userHasSeenNotice).to.be(true); + expect(_source?.telemetry.userHasSeenNotice).to.be(true); }); }); } diff --git a/x-pack/test/case_api_integration/basic/tests/cases/patch_cases.ts b/x-pack/test/case_api_integration/basic/tests/cases/patch_cases.ts index 950fde37e30784..c202111f0e5e4d 100644 --- a/x-pack/test/case_api_integration/basic/tests/cases/patch_cases.ts +++ b/x-pack/test/case_api_integration/basic/tests/cases/patch_cases.ts @@ -438,10 +438,10 @@ export default ({ getService }: FtrProviderContext): void => { }); // There should be no change in their status since syncing is disabled - expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source?.signal.status).to.be( CaseStatuses.open ); - expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source?.signal.status).to.be( CaseStatuses.open ); @@ -471,10 +471,10 @@ export default ({ getService }: FtrProviderContext): void => { }); // There should still be no change in their status since syncing is disabled - expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source?.signal.status).to.be( CaseStatuses.open ); - expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source?.signal.status).to.be( CaseStatuses.open ); @@ -500,10 +500,10 @@ export default ({ getService }: FtrProviderContext): void => { }); // alerts should be updated now that the - expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source?.signal.status).to.be( CaseStatuses.closed ); - expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source?.signal.status).to.be( CaseStatuses['in-progress'] ); }); @@ -573,10 +573,10 @@ export default ({ getService }: FtrProviderContext): void => { let signals = await getSignals(); // There should be no change in their status since syncing is disabled expect( - signals.get(defaultSignalsIndex)?.get(signalIDInFirstIndex)?._source.signal.status + signals.get(defaultSignalsIndex)?.get(signalIDInFirstIndex)?._source?.signal.status ).to.be(CaseStatuses.open); expect( - signals.get(signalsIndex2)?.get(signalIDInSecondIndex)?._source.signal.status + signals.get(signalsIndex2)?.get(signalIDInSecondIndex)?._source?.signal.status ).to.be(CaseStatuses.open); const updatedIndWithStatus: CasesResponse = (await setStatus({ @@ -597,10 +597,10 @@ export default ({ getService }: FtrProviderContext): void => { // There should still be no change in their status since syncing is disabled expect( - signals.get(defaultSignalsIndex)?.get(signalIDInFirstIndex)?._source.signal.status + signals.get(defaultSignalsIndex)?.get(signalIDInFirstIndex)?._source?.signal.status ).to.be(CaseStatuses.open); expect( - signals.get(signalsIndex2)?.get(signalIDInSecondIndex)?._source.signal.status + signals.get(signalsIndex2)?.get(signalIDInSecondIndex)?._source?.signal.status ).to.be(CaseStatuses.open); // turn on the sync settings @@ -623,15 +623,15 @@ export default ({ getService }: FtrProviderContext): void => { // alerts should be updated now that the expect( - signals.get(defaultSignalsIndex)?.get(signalIDInFirstIndex)?._source.signal.status + signals.get(defaultSignalsIndex)?.get(signalIDInFirstIndex)?._source?.signal.status ).to.be(CaseStatuses.closed); expect( - signals.get(signalsIndex2)?.get(signalIDInSecondIndex)?._source.signal.status + signals.get(signalsIndex2)?.get(signalIDInSecondIndex)?._source?.signal.status ).to.be(CaseStatuses.closed); // the duplicate signal id in the other index should not be affect (so its status should be open) expect( - signals.get(defaultSignalsIndex)?.get(signalIDInSecondIndex)?._source.signal.status + signals.get(defaultSignalsIndex)?.get(signalIDInSecondIndex)?._source?.signal.status ).to.be(CaseStatuses.open); }); }); diff --git a/x-pack/test/case_api_integration/basic/tests/cases/sub_cases/patch_sub_cases.ts b/x-pack/test/case_api_integration/basic/tests/cases/sub_cases/patch_sub_cases.ts index d647bb09f804a0..e5cc2489a12e99 100644 --- a/x-pack/test/case_api_integration/basic/tests/cases/sub_cases/patch_sub_cases.ts +++ b/x-pack/test/case_api_integration/basic/tests/cases/sub_cases/patch_sub_cases.ts @@ -96,7 +96,7 @@ export default function ({ getService }: FtrProviderContext) { let signals = await getSignalsWithES({ es, indices: defaultSignalsIndex, ids: signalID }); - expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source?.signal.status).to.be( CaseStatuses.open ); @@ -116,7 +116,7 @@ export default function ({ getService }: FtrProviderContext) { signals = await getSignalsWithES({ es, indices: defaultSignalsIndex, ids: signalID }); - expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source?.signal.status).to.be( CaseStatuses['in-progress'] ); }); @@ -156,10 +156,10 @@ export default function ({ getService }: FtrProviderContext) { ids: [signalID, signalID2], }); - expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source?.signal.status).to.be( CaseStatuses.open ); - expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source?.signal.status).to.be( CaseStatuses.open ); @@ -183,10 +183,10 @@ export default function ({ getService }: FtrProviderContext) { ids: [signalID, signalID2], }); - expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source?.signal.status).to.be( CaseStatuses['in-progress'] ); - expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source?.signal.status).to.be( CaseStatuses['in-progress'] ); }); @@ -244,10 +244,10 @@ export default function ({ getService }: FtrProviderContext) { }); // There should be no change in their status since syncing is disabled - expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source?.signal.status).to.be( CaseStatuses.open ); - expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source?.signal.status).to.be( CaseStatuses.open ); @@ -272,10 +272,10 @@ export default function ({ getService }: FtrProviderContext) { }); // There still should be no change in their status since syncing is disabled - expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source?.signal.status).to.be( CaseStatuses.open ); - expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source?.signal.status).to.be( CaseStatuses.open ); @@ -302,10 +302,10 @@ export default function ({ getService }: FtrProviderContext) { ids: [signalID, signalID2], }); - expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source?.signal.status).to.be( CaseStatuses.closed ); - expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source?.signal.status).to.be( CaseStatuses['in-progress'] ); }); @@ -366,10 +366,10 @@ export default function ({ getService }: FtrProviderContext) { }); // There should be no change in their status since syncing is disabled - expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source?.signal.status).to.be( CaseStatuses.open ); - expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source?.signal.status).to.be( CaseStatuses.open ); @@ -408,10 +408,10 @@ export default function ({ getService }: FtrProviderContext) { }); // There should still be no change in their status since syncing is disabled - expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source?.signal.status).to.be( CaseStatuses.open ); - expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source?.signal.status).to.be( CaseStatuses.open ); @@ -453,10 +453,10 @@ export default function ({ getService }: FtrProviderContext) { }); // alerts should be updated now that the - expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID)?._source?.signal.status).to.be( CaseStatuses['in-progress'] ); - expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source.signal.status).to.be( + expect(signals.get(defaultSignalsIndex)?.get(signalID2)?._source?.signal.status).to.be( CaseStatuses.closed ); }); diff --git a/x-pack/test/case_api_integration/common/lib/utils.ts b/x-pack/test/case_api_integration/common/lib/utils.ts index 96806af37e2c12..6fb108f69ad220 100644 --- a/x-pack/test/case_api_integration/common/lib/utils.ts +++ b/x-pack/test/case_api_integration/common/lib/utils.ts @@ -6,10 +6,11 @@ */ import expect from '@kbn/expect'; -import { Client } from '@elastic/elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; + import * as st from 'supertest'; import supertestAsPromised from 'supertest-as-promised'; -import { Explanation, SearchResponse } from 'elasticsearch'; import { CASES_URL, SUB_CASES_PATCH_DEL_URL } from '../../../../plugins/cases/common/constants'; import { CasesConfigureRequest, @@ -28,19 +29,11 @@ import { getSubCasesUrl } from '../../../../plugins/cases/common/api/helpers'; import { ContextTypeGeneratedAlertType } from '../../../../plugins/cases/server/connectors'; import { SignalHit } from '../../../../plugins/security_solution/server/lib/detection_engine/signals/types'; -interface Hit { - _index: string; - _type: string; - _id: string; - _score: number; - _source: T; - _version?: number; - _explanation?: Explanation; - fields?: any; - highlight?: any; - inner_hits?: any; - matched_queries?: string[]; - sort?: string[]; +function toArray(input: T | T[]): T[] { + if (Array.isArray(input)) { + return input; + } + return [input]; } /** @@ -51,11 +44,11 @@ export const getSignalsWithES = async ({ indices, ids, }: { - es: Client; + es: KibanaClient; indices: string | string[]; ids: string | string[]; -}): Promise>>> => { - const signals = await es.search>({ +}): Promise>>> => { + const signals = await es.search({ index: indices, body: { size: 10000, @@ -64,7 +57,7 @@ export const getSignalsWithES = async ({ filter: [ { ids: { - values: ids, + values: toArray(ids), }, }, ], @@ -76,13 +69,13 @@ export const getSignalsWithES = async ({ return signals.body.hits.hits.reduce((acc, hit) => { let indexMap = acc.get(hit._index); if (indexMap === undefined) { - indexMap = new Map>([[hit._id, hit]]); + indexMap = new Map>([[hit._id, hit]]); } else { indexMap.set(hit._id, hit); } acc.set(hit._index, indexMap); return acc; - }, new Map>>()); + }, new Map>>()); }; interface SetStatusCasesParams { @@ -346,7 +339,7 @@ export const removeServerGeneratedPropertiesFromConfigure = ( return rest; }; -export const deleteAllCaseItems = async (es: Client) => { +export const deleteAllCaseItems = async (es: KibanaClient) => { await Promise.all([ deleteCases(es), deleteSubCases(es), @@ -356,9 +349,10 @@ export const deleteAllCaseItems = async (es: Client) => { ]); }; -export const deleteCasesUserActions = async (es: Client): Promise => { +export const deleteCasesUserActions = async (es: KibanaClient): Promise => { await es.deleteByQuery({ index: '.kibana', + // @ts-expect-error @elastic/elasticsearch DeleteByQueryRequest doesn't accept q parameter q: 'type:cases-user-actions', wait_for_completion: true, refresh: true, @@ -366,9 +360,10 @@ export const deleteCasesUserActions = async (es: Client): Promise => { }); }; -export const deleteCases = async (es: Client): Promise => { +export const deleteCases = async (es: KibanaClient): Promise => { await es.deleteByQuery({ index: '.kibana', + // @ts-expect-error @elastic/elasticsearch DeleteByQueryRequest doesn't accept q parameter q: 'type:cases', wait_for_completion: true, refresh: true, @@ -380,9 +375,10 @@ export const deleteCases = async (es: Client): Promise => { * Deletes all sub cases in the .kibana index. This uses ES to perform the delete and does * not go through the case API. */ -export const deleteSubCases = async (es: Client): Promise => { +export const deleteSubCases = async (es: KibanaClient): Promise => { await es.deleteByQuery({ index: '.kibana', + // @ts-expect-error @elastic/elasticsearch DeleteByQueryRequest doesn't accept q parameter q: 'type:cases-sub-case', wait_for_completion: true, refresh: true, @@ -390,9 +386,10 @@ export const deleteSubCases = async (es: Client): Promise => { }); }; -export const deleteComments = async (es: Client): Promise => { +export const deleteComments = async (es: KibanaClient): Promise => { await es.deleteByQuery({ index: '.kibana', + // @ts-expect-error @elastic/elasticsearch DeleteByQueryRequest doesn't accept q parameter q: 'type:cases-comments', wait_for_completion: true, refresh: true, @@ -400,9 +397,10 @@ export const deleteComments = async (es: Client): Promise => { }); }; -export const deleteConfiguration = async (es: Client): Promise => { +export const deleteConfiguration = async (es: KibanaClient): Promise => { await es.deleteByQuery({ index: '.kibana', + // @ts-expect-error @elastic/elasticsearch DeleteByQueryRequest doesn't accept q parameter q: 'type:cases-configure', wait_for_completion: true, refresh: true, diff --git a/x-pack/test/detection_engine_api_integration/security_and_spaces/tests/create_signals_migrations.ts b/x-pack/test/detection_engine_api_integration/security_and_spaces/tests/create_signals_migrations.ts index dd0052b03382a9..8f7d2a0c01771c 100644 --- a/x-pack/test/detection_engine_api_integration/security_and_spaces/tests/create_signals_migrations.ts +++ b/x-pack/test/detection_engine_api_integration/security_and_spaces/tests/create_signals_migrations.ts @@ -99,6 +99,7 @@ export default ({ getService }: FtrProviderContext): void => { const { body: migrationResults } = await es.search({ index: newIndex }); expect(migrationResults.hits.hits).length(1); + // @ts-expect-error _source has unknown type const migratedSignal = migrationResults.hits.hits[0]._source.signal; expect(migratedSignal._meta.version).to.equal(SIGNALS_TEMPLATE_VERSION); }); diff --git a/x-pack/test/detection_engine_api_integration/utils.ts b/x-pack/test/detection_engine_api_integration/utils.ts index 683d57081a2674..a9c128ee877034 100644 --- a/x-pack/test/detection_engine_api_integration/utils.ts +++ b/x-pack/test/detection_engine_api_integration/utils.ts @@ -6,7 +6,8 @@ */ import { KbnClient } from '@kbn/test'; -import { ApiResponse, Client } from '@elastic/elasticsearch'; +import type { ApiResponse } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { SuperTest } from 'supertest'; import supertestAsPromised from 'supertest-as-promised'; import { Context } from '@elastic/elasticsearch/lib/Transport'; @@ -383,7 +384,7 @@ export const deleteAllAlerts = async ( ); }; -export const downgradeImmutableRule = async (es: Client, ruleId: string): Promise => { +export const downgradeImmutableRule = async (es: KibanaClient, ruleId: string): Promise => { return countDownES(async () => { return es.updateByQuery({ index: '.kibana', @@ -408,9 +409,10 @@ export const downgradeImmutableRule = async (es: Client, ruleId: string): Promis * Remove all timelines from the .kibana index * @param es The ElasticSearch handle */ -export const deleteAllTimelines = async (es: Client): Promise => { +export const deleteAllTimelines = async (es: KibanaClient): Promise => { await es.deleteByQuery({ index: '.kibana', + // @ts-expect-error @elastic/elasticsearch DeleteByQueryRequest doesn't accept q parameter q: 'type:siem-ui-timeline', wait_for_completion: true, refresh: true, @@ -423,10 +425,11 @@ export const deleteAllTimelines = async (es: Client): Promise => { * This will retry 20 times before giving up and hopefully still not interfere with other tests * @param es The ElasticSearch handle */ -export const deleteAllRulesStatuses = async (es: Client): Promise => { +export const deleteAllRulesStatuses = async (es: KibanaClient): Promise => { return countDownES(async () => { return es.deleteByQuery({ index: '.kibana', + // @ts-expect-error @elastic/elasticsearch DeleteByQueryRequest doesn't accept q parameter q: 'type:siem-detection-engine-rule-status', wait_for_completion: true, refresh: true, @@ -1176,7 +1179,7 @@ export const getIndexNameFromLoad = (loadResponse: Record): str * @param esClient elasticsearch {@link Client} * @param index name of the index to query */ -export const waitForIndexToPopulate = async (es: Client, index: string): Promise => { +export const waitForIndexToPopulate = async (es: KibanaClient, index: string): Promise => { await waitFor(async () => { const response = await es.count<{ count: number }>({ index }); return response.body.count > 0; diff --git a/x-pack/test/fleet_api_integration/apis/agents/acks.ts b/x-pack/test/fleet_api_integration/apis/agents/acks.ts index 6b68d8b28a43ea..427c9c2394d9d1 100644 --- a/x-pack/test/fleet_api_integration/apis/agents/acks.ts +++ b/x-pack/test/fleet_api_integration/apis/agents/acks.ts @@ -35,6 +35,7 @@ export default function (providerContext: FtrProviderContext) { index: '.fleet-agents', id: 'agent1', }); + // @ts-expect-error has unknown type agentDoc.access_api_key_id = apiKey.id; await esClient.update({ index: '.fleet-agents', @@ -239,11 +240,11 @@ export default function (providerContext: FtrProviderContext) { }) .expect(200); - const res = await esClient.get({ + const res = await esClient.get<{ upgraded_at: unknown }>({ index: '.fleet-agents', id: 'agent1', }); - expect(res.body._source.upgraded_at).to.be.ok(); + expect(res.body._source?.upgraded_at).to.be.ok(); }); }); } diff --git a/x-pack/test/fleet_api_integration/apis/agents/checkin.ts b/x-pack/test/fleet_api_integration/apis/agents/checkin.ts index 778063dff72c70..9150b81abf366c 100644 --- a/x-pack/test/fleet_api_integration/apis/agents/checkin.ts +++ b/x-pack/test/fleet_api_integration/apis/agents/checkin.ts @@ -38,6 +38,7 @@ export default function (providerContext: FtrProviderContext) { index: '.fleet-agents', id: 'agent1', }); + // @ts-expect-error agentDoc has unknown type agentDoc.access_api_key_id = apiKey.id; await esClient.update({ index: '.fleet-agents', diff --git a/x-pack/test/fleet_api_integration/apis/agents/unenroll.ts b/x-pack/test/fleet_api_integration/apis/agents/unenroll.ts index 8924c090da0891..09a0d3c927e4cb 100644 --- a/x-pack/test/fleet_api_integration/apis/agents/unenroll.ts +++ b/x-pack/test/fleet_api_integration/apis/agents/unenroll.ts @@ -45,9 +45,11 @@ export default function (providerContext: FtrProviderContext) { index: '.fleet-agents', id: 'agent1', }); - // @ts-ignore + // @ts-expect-error agentDoc has unknown type agentDoc.access_api_key_id = accessAPIKeyId; + // @ts-expect-error agentDoc has unknown type agentDoc.default_api_key_id = outputAPIKeyBody.id; + // @ts-expect-error agentDoc has unknown type agentDoc.default_api_key = Buffer.from( `${outputAPIKeyBody.id}:${outputAPIKeyBody.api_key}` ).toString('base64'); diff --git a/x-pack/test/fleet_api_integration/apis/fleet_setup.ts b/x-pack/test/fleet_api_integration/apis/fleet_setup.ts index 237b9617dafb90..c9709475d182d9 100644 --- a/x-pack/test/fleet_api_integration/apis/fleet_setup.ts +++ b/x-pack/test/fleet_api_integration/apis/fleet_setup.ts @@ -80,6 +80,7 @@ export default function (providerContext: FtrProviderContext) { applications: [], run_as: [], metadata: {}, + // @ts-expect-error @elastic/elasticsearch PutRoleRequest.body doesn't declare transient_metadata property transient_metadata: { enabled: true }, }, }); diff --git a/x-pack/test/functional/apps/ml/anomaly_detection/aggregated_scripted_job.ts b/x-pack/test/functional/apps/ml/anomaly_detection/aggregated_scripted_job.ts index e63aff3a081864..980060fea1c472 100644 --- a/x-pack/test/functional/apps/ml/anomaly_detection/aggregated_scripted_job.ts +++ b/x-pack/test/functional/apps/ml/anomaly_detection/aggregated_scripted_job.ts @@ -16,6 +16,7 @@ export default function ({ getService }: FtrProviderContext) { const supportedTestSuites = [ { suiteTitle: 'supported job with aggregation field', + // @ts-expect-error not convertable to Job type jobConfig: { job_id: `fq_supported_aggs_${ts}`, job_type: 'anomaly_detector', @@ -102,6 +103,7 @@ export default function ({ getService }: FtrProviderContext) { }, { suiteTitle: 'supported job with scripted field', + // @ts-expect-error not convertable to Job type jobConfig: { job_id: `fq_supported_script_${ts}`, job_type: 'anomaly_detector', @@ -176,6 +178,7 @@ export default function ({ getService }: FtrProviderContext) { const unsupportedTestSuites = [ { suiteTitle: 'unsupported job with bucket_script aggregation field', + // @ts-expect-error not convertable to Job type jobConfig: { job_id: `fq_unsupported_aggs_${ts}`, job_type: 'anomaly_detector', @@ -280,6 +283,7 @@ export default function ({ getService }: FtrProviderContext) { }, { suiteTitle: 'unsupported job with partition by of a scripted field', + // @ts-expect-error not convertable to Job type jobConfig: { job_id: `fq_unsupported_script_${ts}`, job_type: 'anomaly_detector', diff --git a/x-pack/test/functional/apps/ml/anomaly_detection/annotations.ts b/x-pack/test/functional/apps/ml/anomaly_detection/annotations.ts index 81d05ee94e6c49..97cc6be95ed934 100644 --- a/x-pack/test/functional/apps/ml/anomaly_detection/annotations.ts +++ b/x-pack/test/functional/apps/ml/anomaly_detection/annotations.ts @@ -8,6 +8,7 @@ import { FtrProviderContext } from '../../../ftr_provider_context'; import { Job, Datafeed } from '../../../../../plugins/ml/common/types/anomaly_detection_jobs'; +// @ts-expect-error doesn't implement the full interface const JOB_CONFIG: Job = { job_id: `fq_single_1_smv`, description: 'mean(responsetime) on farequote dataset with 15m bucket span', @@ -27,6 +28,7 @@ const JOB_CONFIG: Job = { model_plot_config: { enabled: true }, }; +// @ts-expect-error doesn't implement the full interface const DATAFEED_CONFIG: Datafeed = { datafeed_id: 'datafeed-fq_single_1_smv', indices: ['ft_farequote'], diff --git a/x-pack/test/functional/apps/ml/anomaly_detection/anomaly_explorer.ts b/x-pack/test/functional/apps/ml/anomaly_detection/anomaly_explorer.ts index ff38544fa8c030..a08a5cfca2afde 100644 --- a/x-pack/test/functional/apps/ml/anomaly_detection/anomaly_explorer.ts +++ b/x-pack/test/functional/apps/ml/anomaly_detection/anomaly_explorer.ts @@ -9,6 +9,7 @@ import expect from '@kbn/expect'; import { FtrProviderContext } from '../../../ftr_provider_context'; import { Job, Datafeed } from '../../../../../plugins/ml/common/types/anomaly_detection_jobs'; +// @ts-expect-error not full interface const JOB_CONFIG: Job = { job_id: `fq_multi_1_ae`, description: @@ -28,6 +29,7 @@ const JOB_CONFIG: Job = { model_plot_config: { enabled: true }, }; +// @ts-expect-error not full interface const DATAFEED_CONFIG: Datafeed = { datafeed_id: 'datafeed-fq_multi_1_se', indices: ['ft_farequote'], diff --git a/x-pack/test/functional/apps/ml/anomaly_detection/single_metric_viewer.ts b/x-pack/test/functional/apps/ml/anomaly_detection/single_metric_viewer.ts index ec25c997e27706..950ad2a702b06f 100644 --- a/x-pack/test/functional/apps/ml/anomaly_detection/single_metric_viewer.ts +++ b/x-pack/test/functional/apps/ml/anomaly_detection/single_metric_viewer.ts @@ -8,6 +8,7 @@ import { FtrProviderContext } from '../../../ftr_provider_context'; import { Job, Datafeed } from '../../../../../plugins/ml/common/types/anomaly_detection_jobs'; +// @ts-expect-error not full interface const JOB_CONFIG: Job = { job_id: `fq_single_1_smv`, description: 'mean(responsetime) on farequote dataset with 15m bucket span', @@ -27,6 +28,7 @@ const JOB_CONFIG: Job = { model_plot_config: { enabled: true }, }; +// @ts-expect-error not full interface const DATAFEED_CONFIG: Datafeed = { datafeed_id: 'datafeed-fq_single_1_smv', indices: ['ft_farequote'], @@ -91,6 +93,7 @@ export default function ({ getService }: FtrProviderContext) { }); describe('with entity fields', function () { + // @ts-expect-error not full interface const jobConfig: Job = { job_id: `ecom_01`, description: @@ -121,6 +124,7 @@ export default function ({ getService }: FtrProviderContext) { model_plot_config: { enabled: true }, }; + // @ts-expect-error not full interface const datafeedConfig: Datafeed = { datafeed_id: 'datafeed-ecom_01', indices: ['ft_ecommerce'], diff --git a/x-pack/test/functional/apps/ml/permissions/full_ml_access.ts b/x-pack/test/functional/apps/ml/permissions/full_ml_access.ts index 8d29b611c0bf54..ac8ff055209c9f 100644 --- a/x-pack/test/functional/apps/ml/permissions/full_ml_access.ts +++ b/x-pack/test/functional/apps/ml/permissions/full_ml_access.ts @@ -136,10 +136,11 @@ export default function ({ getService }: FtrProviderContext) { description: 'Test calendar', }); await ml.api.createCalendarEvents(calendarId, [ + // @ts-expect-error not full interface { description: eventDescription, - start_time: 1513641600000, - end_time: 1513728000000, + start_time: '1513641600000', + end_time: '1513728000000', }, ]); diff --git a/x-pack/test/functional/apps/ml/permissions/read_ml_access.ts b/x-pack/test/functional/apps/ml/permissions/read_ml_access.ts index 71ac9c00032dcd..95d0d209164296 100644 --- a/x-pack/test/functional/apps/ml/permissions/read_ml_access.ts +++ b/x-pack/test/functional/apps/ml/permissions/read_ml_access.ts @@ -137,10 +137,11 @@ export default function ({ getService }: FtrProviderContext) { description: 'Test calendar', }); await ml.api.createCalendarEvents(calendarId, [ + // @ts-expect-error not full interface { description: eventDescription, - start_time: 1513641600000, - end_time: 1513728000000, + start_time: '1513641600000', + end_time: '1513728000000', }, ]); diff --git a/x-pack/test/functional/apps/ml/settings/calendar_creation.ts b/x-pack/test/functional/apps/ml/settings/calendar_creation.ts index 968eafab21ab47..2beae2ad007b57 100644 --- a/x-pack/test/functional/apps/ml/settings/calendar_creation.ts +++ b/x-pack/test/functional/apps/ml/settings/calendar_creation.ts @@ -21,6 +21,7 @@ export default function ({ getService }: FtrProviderContext) { await ml.testResources.createIndexPatternIfNeeded('ft_farequote', '@timestamp'); await asyncForEach(jobConfigs, async (jobConfig) => { + // @ts-expect-error not full interface await ml.api.createAnomalyDetectionJob(jobConfig); }); await ml.testResources.setKibanaTimeZoneToUTC(); diff --git a/x-pack/test/functional/apps/ml/settings/calendar_edit.ts b/x-pack/test/functional/apps/ml/settings/calendar_edit.ts index 9242c2e1b594c1..1237cd8d71b8c4 100644 --- a/x-pack/test/functional/apps/ml/settings/calendar_edit.ts +++ b/x-pack/test/functional/apps/ml/settings/calendar_edit.ts @@ -27,6 +27,7 @@ export default function ({ getService }: FtrProviderContext) { await ml.testResources.createIndexPatternIfNeeded('ft_farequote', '@timestamp'); await asyncForEach(jobConfigs, async (jobConfig) => { + // @ts-expect-error not full interface await ml.api.createAnomalyDetectionJob(jobConfig); }); @@ -34,6 +35,7 @@ export default function ({ getService }: FtrProviderContext) { job_ids: jobConfigs.map((c) => c.job_id), description: 'Test calendar', }); + // @ts-expect-error not full interface await ml.api.createCalendarEvents(calendarId, testEvents); await ml.testResources.setKibanaTimeZoneToUTC(); diff --git a/x-pack/test/functional/services/ml/api.ts b/x-pack/test/functional/services/ml/api.ts index c9cfe1fee4ef9a..7d09deff6f6b77 100644 --- a/x-pack/test/functional/services/ml/api.ts +++ b/x-pack/test/functional/services/ml/api.ts @@ -5,10 +5,10 @@ * 2.0. */ +import { estypes } from '@elastic/elasticsearch'; import expect from '@kbn/expect'; import { ProvidedType } from '@kbn/test/types/ftr'; -import { IndexDocumentParams } from 'elasticsearch'; -import { Calendar, CalendarEvent } from '../../../../plugins/ml/server/models/calendar/index'; +import { Calendar } from '../../../../plugins/ml/server/models/calendar/index'; import { Annotation } from '../../../../plugins/ml/common/types/annotations'; import { DataFrameAnalyticsConfig } from '../../../../plugins/ml/public/application/data_frame_analytics/common'; import { FtrProviderContext } from '../../ftr_provider_context'; @@ -24,18 +24,8 @@ import { } from '../../../../plugins/ml/common/constants/index_patterns'; import { COMMON_REQUEST_HEADERS } from '../../../functional/services/ml/common_api'; -interface EsIndexResult { - _index: string; - _id: string; - _version: number; - result: string; - _shards: any; - _seq_no: number; - _primary_term: number; -} - export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { - const es = getService('legacyEs'); + const es = getService('es'); const log = getService('log'); const retry = getService('retry'); const esSupertest = getService('esSupertest'); @@ -44,7 +34,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { return { async hasJobResults(jobId: string): Promise { - const response = await es.search({ + const { body } = await es.search({ index: '.ml-anomalies-*', body: { size: 1, @@ -56,7 +46,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { }, }); - return response.hits.hits.length > 0; + return body.hits.hits.length > 0; }, async assertJobResultsExist(jobId: string) { @@ -84,7 +74,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { }, async hasDetectorResults(jobId: string, detectorIndex: number): Promise { - const response = await es.search({ + const { body } = await es.search({ index: '.ml-anomalies-*', body: { size: 1, @@ -112,7 +102,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { }, }); - return response.hits.hits.length > 0; + return body.hits.hits.length > 0; }, async assertDetectorResultsExist(jobId: string, detectorIndex: number) { @@ -133,13 +123,13 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { async createIndices(indices: string) { log.debug(`Creating indices: '${indices}'...`); - if ((await es.indices.exists({ index: indices, allowNoIndices: false })) === true) { + if ((await es.indices.exists({ index: indices, allow_no_indices: false })).body === true) { log.debug(`Indices '${indices}' already exist. Nothing to create.`); return; } - const createResponse = await es.indices.create({ index: indices }); - expect(createResponse) + const { body } = await es.indices.create({ index: indices }); + expect(body) .to.have.property('acknowledged') .eql(true, 'Response for create request indices should be acknowledged.'); @@ -149,15 +139,15 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { async deleteIndices(indices: string) { log.debug(`Deleting indices: '${indices}'...`); - if ((await es.indices.exists({ index: indices, allowNoIndices: false })) === false) { + if ((await es.indices.exists({ index: indices, allow_no_indices: false })).body === false) { log.debug(`Indices '${indices}' don't exist. Nothing to delete.`); return; } - const deleteResponse = await es.indices.delete({ + const { body } = await es.indices.delete({ index: indices, }); - expect(deleteResponse) + expect(body) .to.have.property('acknowledged') .eql(true, 'Response for delete request should be acknowledged.'); @@ -316,7 +306,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { async assertIndicesExist(indices: string) { await retry.tryForTime(30 * 1000, async () => { - if ((await es.indices.exists({ index: indices, allowNoIndices: false })) === true) { + if ((await es.indices.exists({ index: indices, allow_no_indices: false })).body === true) { return true; } else { throw new Error(`indices '${indices}' should exist`); @@ -326,7 +316,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { async assertIndicesNotToExist(indices: string) { await retry.tryForTime(30 * 1000, async () => { - if ((await es.indices.exists({ index: indices, allowNoIndices: false })) === false) { + if ((await es.indices.exists({ index: indices, allow_no_indices: false })).body === false) { return true; } else { throw new Error(`indices '${indices}' should not exist`); @@ -336,14 +326,14 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { async assertIndicesNotEmpty(indices: string) { await retry.tryForTime(30 * 1000, async () => { - const response = await es.search({ + const { body } = await es.search({ index: indices, body: { size: 1, }, }); - if (response.hits.hits.length > 0) { + if (body.hits.hits.length > 0) { return true; } else { throw new Error(`indices '${indices}' should not be empty`); @@ -393,7 +383,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { }); }, - async createCalendarEvents(calendarId: string, events: CalendarEvent[]) { + async createCalendarEvents(calendarId: string, events: estypes.ScheduledEvent[]) { log.debug(`Creating events for calendar with id '${calendarId}'...`); await esSupertest.post(`/_ml/calendars/${calendarId}/events`).send({ events }).expect(200); await this.waitForEventsToExistInCalendar(calendarId, events); @@ -405,10 +395,10 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { }, assertAllEventsExistInCalendar: ( - eventsToCheck: CalendarEvent[], + eventsToCheck: estypes.ScheduledEvent[], calendar: Calendar ): boolean => { - const updatedCalendarEvents = calendar.events as CalendarEvent[]; + const updatedCalendarEvents = calendar.events; let allEventsAreUpdated = true; for (const eventToCheck of eventsToCheck) { // if at least one of the events that we need to check is not in the updated events @@ -417,8 +407,10 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { updatedCalendarEvents.findIndex( (updatedEvent) => updatedEvent.description === eventToCheck.description && - updatedEvent.start_time === eventToCheck.start_time && - updatedEvent.end_time === eventToCheck.end_time + // updatedEvent are fetched with suptertest which converts start_time and end_time to number + // sometimes eventToCheck declared manually with types incompatible with estypes.ScheduledEvent + String(updatedEvent.start_time) === String(eventToCheck.start_time) && + String(updatedEvent.end_time) === String(eventToCheck.end_time) ) < 0 ) { allEventsAreUpdated = false; @@ -436,7 +428,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { async waitForEventsToExistInCalendar( calendarId: string, - eventsToCheck: CalendarEvent[], + eventsToCheck: estypes.ScheduledEvent[], errorMsg?: string ) { await retry.waitForWithTimeout(`'${calendarId}' events to exist`, 5 * 1000, async () => { @@ -805,7 +797,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { async getAnnotations(jobId: string) { log.debug(`Fetching annotations for job '${jobId}'...`); - const results = await es.search({ + const { body } = await es.search({ index: ML_ANNOTATIONS_INDEX_ALIAS_READ, body: { query: { @@ -815,16 +807,16 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { }, }, }); - expect(results).to.not.be(undefined); - expect(results).to.have.property('hits'); + expect(body).to.not.be(undefined); + expect(body).to.have.property('hits'); log.debug('> Annotations fetched.'); - return results.hits.hits; + return body.hits.hits; }, async getAnnotationById(annotationId: string): Promise { log.debug(`Fetching annotation '${annotationId}'...`); - const result = await es.search({ + const { body } = await es.search({ index: ML_ANNOTATIONS_INDEX_ALIAS_READ, body: { size: 1, @@ -836,9 +828,10 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { }, }); log.debug('> Annotation fetched.'); - // @ts-ignore due to outdated type for hits.total - if (result.hits.total.value === 1) { - return result?.hits?.hits[0]?._source as Annotation; + + // @ts-expect-error doesn't handle total as number + if (body.hits.total.value === 1) { + return body?.hits?.hits[0]?._source as Annotation; } return undefined; }, @@ -846,15 +839,15 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { async indexAnnotation(annotationRequestBody: Partial) { log.debug(`Indexing annotation '${JSON.stringify(annotationRequestBody)}'...`); // @ts-ignore due to outdated type for IndexDocumentParams.type - const params: IndexDocumentParams> = { + const params = { index: ML_ANNOTATIONS_INDEX_ALIAS_WRITE, body: annotationRequestBody, refresh: 'wait_for', - }; - const results: EsIndexResult = await es.index(params); - await this.waitForAnnotationToExist(results._id); + } as const; + const { body } = await es.index(params); + await this.waitForAnnotationToExist(body._id); log.debug('> Annotation indexed.'); - return results; + return body; }, async waitForAnnotationToExist(annotationId: string, errorMsg?: string) { diff --git a/x-pack/test/functional/services/ml/common_config.ts b/x-pack/test/functional/services/ml/common_config.ts index a16c0b88de900a..af3d393a16754e 100644 --- a/x-pack/test/functional/services/ml/common_config.ts +++ b/x-pack/test/functional/services/ml/common_config.ts @@ -12,6 +12,7 @@ import { FtrProviderContext } from '../../ftr_provider_context'; import { Job, Datafeed } from '../../../../plugins/ml/common/types/anomaly_detection_jobs'; import { DataFrameAnalyticsConfig } from '../../../../plugins/ml/public/application/data_frame_analytics/common'; +// @ts-expect-error not full interface const FQ_SM_JOB_CONFIG: Job = { job_id: ``, description: 'mean(responsetime) on farequote dataset with 15m bucket span', @@ -31,6 +32,7 @@ const FQ_SM_JOB_CONFIG: Job = { model_plot_config: { enabled: true }, }; +// @ts-expect-error not full interface const FQ_MM_JOB_CONFIG: Job = { job_id: `fq_multi_1_ae`, description: @@ -50,6 +52,7 @@ const FQ_MM_JOB_CONFIG: Job = { model_plot_config: { enabled: true }, }; +// @ts-expect-error not full interface const FQ_DATAFEED_CONFIG: Datafeed = { datafeed_id: '', indices: ['ft_farequote'], diff --git a/x-pack/test/functional_with_es_ssl/apps/ml/alert_flyout.ts b/x-pack/test/functional_with_es_ssl/apps/ml/alert_flyout.ts index c3859e1044b4fa..8fcf8be9fa4930 100644 --- a/x-pack/test/functional_with_es_ssl/apps/ml/alert_flyout.ts +++ b/x-pack/test/functional_with_es_ssl/apps/ml/alert_flyout.ts @@ -80,8 +80,10 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { testJobId = job.job_id; // Set up jobs + // @ts-expect-error not full interface await ml.api.createAnomalyDetectionJob(job); await ml.api.openAnomalyDetectionJob(job.job_id); + // @ts-expect-error not full interface await ml.api.createDatafeed(datafeed); await ml.api.startDatafeed(datafeed.datafeed_id); await ml.api.waitForDatafeedState(datafeed.datafeed_id, DATAFEED_STATE.STARTED); diff --git a/x-pack/test/lists_api_integration/utils.ts b/x-pack/test/lists_api_integration/utils.ts index 7a0b1b7f7934a6..0512cede0a84f6 100644 --- a/x-pack/test/lists_api_integration/utils.ts +++ b/x-pack/test/lists_api_integration/utils.ts @@ -7,7 +7,7 @@ import { SuperTest } from 'supertest'; import supertestAsPromised from 'supertest-as-promised'; -import { Client } from '@elastic/elasticsearch'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { getImportListItemAsBuffer } from '../../plugins/lists/common/schemas/request/import_list_item_schema.mock'; import { @@ -157,10 +157,11 @@ export const binaryToString = (res: any, callback: any): void => { * This will retry 20 times before giving up and hopefully still not interfere with other tests * @param es The ElasticSearch handle */ -export const deleteAllExceptions = async (es: Client): Promise => { +export const deleteAllExceptions = async (es: KibanaClient): Promise => { return countDownES(async () => { return es.deleteByQuery({ index: '.kibana', + // @ts-expect-error @elastic/elasticsearch DeleteByQueryRequest doesn't accept q parameter q: 'type:exception-list or type:exception-list-agnostic', wait_for_completion: true, refresh: true, diff --git a/x-pack/test/observability_api_integration/trial/tests/annotations.ts b/x-pack/test/observability_api_integration/trial/tests/annotations.ts index 83950a24afa7da..9435c1f49b1ca1 100644 --- a/x-pack/test/observability_api_integration/trial/tests/annotations.ts +++ b/x-pack/test/observability_api_integration/trial/tests/annotations.ts @@ -8,7 +8,6 @@ import expect from '@kbn/expect'; import { JsonObject } from 'src/plugins/kibana_utils/common'; import { Annotation } from '../../../../plugins/observability/common/annotations'; -import { ESSearchHit } from '../../../../../typings/elasticsearch'; import { FtrProviderContext } from '../../common/ftr_provider_context'; const DEFAULT_INDEX_NAME = 'observability-annotations'; @@ -154,6 +153,7 @@ export default function annotationApiTests({ getService }: FtrProviderContext) { track_total_hits: true, }); + // @ts-expect-error doesn't handle number expect(search.body.hits.total.value).to.be(1); expect(search.body.hits.hits[0]._source).to.eql(response.body._source); @@ -237,16 +237,15 @@ export default function annotationApiTests({ getService }: FtrProviderContext) { }, }); - const initialSearch = await es.search({ + const initialSearch = await es.search({ index: DEFAULT_INDEX_NAME, track_total_hits: true, }); + // @ts-expect-error doesn't handler number expect(initialSearch.body.hits.total.value).to.be(2); - const [id1, id2] = initialSearch.body.hits.hits.map( - (hit: ESSearchHit) => hit._id - ); + const [id1, id2] = initialSearch.body.hits.hits.map((hit) => hit._id); expect( ( @@ -262,6 +261,7 @@ export default function annotationApiTests({ getService }: FtrProviderContext) { track_total_hits: true, }); + // @ts-expect-error doesn't handler number expect(searchAfterFirstDelete.body.hits.total.value).to.be(1); expect(searchAfterFirstDelete.body.hits.hits[0]._id).to.be(id2); @@ -280,6 +280,7 @@ export default function annotationApiTests({ getService }: FtrProviderContext) { track_total_hits: true, }); + // @ts-expect-error doesn't handle number expect(searchAfterSecondDelete.body.hits.total.value).to.be(0); }); }); diff --git a/x-pack/test/saved_object_api_integration/common/lib/create_users_and_roles.ts b/x-pack/test/saved_object_api_integration/common/lib/create_users_and_roles.ts index 38e566a7305069..2ba5ce3f7d5b17 100644 --- a/x-pack/test/saved_object_api_integration/common/lib/create_users_and_roles.ts +++ b/x-pack/test/saved_object_api_integration/common/lib/create_users_and_roles.ts @@ -5,11 +5,11 @@ * 2.0. */ -import { SuperTest } from 'supertest'; -import { Client } from '@elastic/elasticsearch'; +import type { SuperTest } from 'supertest'; +import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; import { AUTHENTICATION } from './authentication'; -export const createUsersAndRoles = async (es: Client, supertest: SuperTest) => { +export const createUsersAndRoles = async (es: KibanaClient, supertest: SuperTest) => { await supertest .put('/api/security/role/kibana_legacy_user') .send({ diff --git a/x-pack/test/security_api_integration/tests/session_idle/cleanup.ts b/x-pack/test/security_api_integration/tests/session_idle/cleanup.ts index 84f84e87521221..bb46beef41449a 100644 --- a/x-pack/test/security_api_integration/tests/session_idle/cleanup.ts +++ b/x-pack/test/security_api_integration/tests/session_idle/cleanup.ts @@ -43,8 +43,10 @@ export default function ({ getService }: FtrProviderContext) { } async function getNumberOfSessionDocuments() { - return (await es.search({ index: '.kibana_security_session*' })).body.hits.total - .value as number; + return ( + // @ts-expect-error doesn't handle total as number + (await es.search({ index: '.kibana_security_session*' })).body.hits.total.value as number + ); } async function loginWithSAML(providerName: string) { diff --git a/x-pack/test/security_api_integration/tests/session_lifespan/cleanup.ts b/x-pack/test/security_api_integration/tests/session_lifespan/cleanup.ts index 3d24efc9b8e749..0b17f037dfbd99 100644 --- a/x-pack/test/security_api_integration/tests/session_lifespan/cleanup.ts +++ b/x-pack/test/security_api_integration/tests/session_lifespan/cleanup.ts @@ -38,8 +38,10 @@ export default function ({ getService }: FtrProviderContext) { } async function getNumberOfSessionDocuments() { - return (await es.search({ index: '.kibana_security_session*' })).body.hits.total - .value as number; + return ( + // @ts-expect-error doesn't handle total as number + (await es.search({ index: '.kibana_security_session*' })).body.hits.total.value as number + ); } async function loginWithSAML(providerName: string) { diff --git a/x-pack/test/security_solution_endpoint_api_int/apis/package.ts b/x-pack/test/security_solution_endpoint_api_int/apis/package.ts index 5d93b820f30164..d46b7723fcafe3 100644 --- a/x-pack/test/security_solution_endpoint_api_int/apis/package.ts +++ b/x-pack/test/security_solution_endpoint_api_int/apis/package.ts @@ -6,7 +6,6 @@ */ import expect from '@kbn/expect'; -import { SearchResponse } from 'elasticsearch'; import { ResolverPaginatedEvents, SafeEndpointEvent, @@ -49,7 +48,7 @@ export default function ({ getService }: FtrProviderContext) { const generator = new EndpointDocGenerator('data'); const searchForID = async (id: string) => { - return es.search>({ + return es.search({ index: eventsIndexPattern, body: { query: { @@ -57,7 +56,7 @@ export default function ({ getService }: FtrProviderContext) { filter: [ { ids: { - values: id, + values: [id], }, }, ], @@ -89,7 +88,7 @@ export default function ({ getService }: FtrProviderContext) { ); // ensure that the event was inserted into ES - expect(eventWithBothIPs.body.hits.hits[0]._source.event?.id).to.be( + expect(eventWithBothIPs.body.hits.hits[0]._source?.event?.id).to.be( eventWithoutNetworkObject.event?.id ); }); @@ -175,7 +174,7 @@ export default function ({ getService }: FtrProviderContext) { it('sets the event.ingested field', async () => { const resp = await searchForID(genData.eventsInfo[0]._id); - expect(resp.body.hits.hits[0]._source.event.ingested).to.not.be(undefined); + expect(resp.body.hits.hits[0]._source?.event.ingested).to.not.be(undefined); }); }); @@ -221,11 +220,11 @@ export default function ({ getService }: FtrProviderContext) { networkIndexData.eventsInfo[0]._id ); // Should be 'United States' - expect(eventWithBothIPs.body.hits.hits[0]._source.source.geo?.country_name).to.not.be( + expect(eventWithBothIPs.body.hits.hits[0]._source?.source.geo?.country_name).to.not.be( undefined ); // should be 'Iceland' - expect(eventWithBothIPs.body.hits.hits[0]._source.destination.geo?.country_name).to.not.be( + expect(eventWithBothIPs.body.hits.hits[0]._source?.destination.geo?.country_name).to.not.be( undefined ); @@ -233,18 +232,18 @@ export default function ({ getService }: FtrProviderContext) { networkIndexData.eventsInfo[1]._id ); // Should be 'United States' - expect(eventWithBothIPs.body.hits.hits[0]._source.source.geo?.country_name).to.not.be( + expect(eventWithBothIPs.body.hits.hits[0]._source?.source.geo?.country_name).to.not.be( undefined ); - expect(eventWithSourceOnly.body.hits.hits[0]._source.destination?.geo).to.be(undefined); + expect(eventWithSourceOnly.body.hits.hits[0]._source?.destination?.geo).to.be(undefined); }); it('does not set geoip fields for events in indices other than the network index', async () => { const eventWithBothIPs = await searchForID( processIndexData.eventsInfo[0]._id ); - expect(eventWithBothIPs.body.hits.hits[0]._source.source.geo).to.be(undefined); - expect(eventWithBothIPs.body.hits.hits[0]._source.destination.geo).to.be(undefined); + expect(eventWithBothIPs.body.hits.hits[0]._source?.source.geo).to.be(undefined); + expect(eventWithBothIPs.body.hits.hits[0]._source?.destination.geo).to.be(undefined); }); }); }); diff --git a/x-pack/test/security_solution_endpoint_api_int/services/resolver.ts b/x-pack/test/security_solution_endpoint_api_int/services/resolver.ts index add6539470cc1c..156043bd3c9183 100644 --- a/x-pack/test/security_solution_endpoint_api_int/services/resolver.ts +++ b/x-pack/test/security_solution_endpoint_api_int/services/resolver.ts @@ -49,14 +49,6 @@ interface BulkCreateHeader { }; } -interface BulkResponse { - items: Array<{ - create: { - _id: string; - }; - }>; -} - export function ResolverGeneratorProvider({ getService }: FtrProviderContext) { const client = getService('es'); @@ -69,12 +61,13 @@ export function ResolverGeneratorProvider({ getService }: FtrProviderContext) { array.push({ create: { _index: eventsIndex } }, doc); return array; }, []); - const bulkResp = await client.bulk({ body, refresh: true }); + const bulkResp = await client.bulk({ body, refresh: true }); const eventsInfo = events.map((event: Event, i: number) => { - return { event, _id: bulkResp.body.items[i].create._id }; + return { event, _id: bulkResp.body.items[i].create?._id }; }); + // @ts-expect-error @elastic/elasticsearch expected BulkResponseItemBase._id: string return { eventsInfo, indices: [eventsIndex] }; }, async createTrees( diff --git a/yarn.lock b/yarn.lock index 2a198b84b5efcb..84a7dd9dcc9a45 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1378,10 +1378,10 @@ version "0.0.0" uid "" -"@elastic/elasticsearch@npm:@elastic/elasticsearch-canary@^7.12.0-canary.4": - version "7.12.0-canary.4" - resolved "https://registry.yarnpkg.com/@elastic/elasticsearch-canary/-/elasticsearch-canary-7.12.0-canary.4.tgz#8a96352c845140c26f453f06ef9aa98acf2a1bed" - integrity sha512-/g/HXJdwAKBYt5jTLX0cVKnCk1DxGtCmb287RAYvN7QYmo/XO9u9hm6lWJiaIx8HJG0AFwEGyeTacf6XszXC1A== +"@elastic/elasticsearch@npm:@elastic/elasticsearch-canary@7.13.0-canary.1": + version "7.13.0-canary.1" + resolved "https://registry.yarnpkg.com/@elastic/elasticsearch-canary/-/elasticsearch-canary-7.13.0-canary.1.tgz#53a8ddbaeaba77dc163aad972e56d6741c98858a" + integrity sha512-RqJxZyouL/lTN4EgBvN9xQKq0zUIzSUAks91q/UvM41XyATTeTa2e9+zxrK07JCi0R6l/xAlHrLglf1/LiJOyg== dependencies: debug "^4.3.1" hpagent "^0.1.1" @@ -24959,9 +24959,9 @@ scss-tokenizer@^0.2.3: source-map "^0.4.2" secure-json-parse@^2.3.1: - version "2.3.2" - resolved "https://registry.yarnpkg.com/secure-json-parse/-/secure-json-parse-2.3.2.tgz#0284c9525c6f24d38774f13727addff80634c2e9" - integrity sha512-4oUSFU0w2d8/XQb7NO9dbMYyp/hxIwZPcZcGAlAAEziMRHs+NbUcx2Z5dda/z8o+avyQ8gpuYnTMlGh8SVwg9g== + version "2.3.1" + resolved "https://registry.yarnpkg.com/secure-json-parse/-/secure-json-parse-2.3.1.tgz#908aa5e806e223ff8d179d37ad95c2433f5f147d" + integrity sha512-5uGhQLHSC9tVa7RGPkSwxbZVsJCZvIODOadAimCXkU1aCa1fWdszj2DktcutK8A7dD58PoRdxTYiy0jFl6qjnw== seed-random@~2.2.0: version "2.2.0"