From c9e56a26bbd7d3725f59b72d7c40dc260f360777 Mon Sep 17 00:00:00 2001 From: Kerry Gallagher Date: Fri, 12 Jun 2020 19:32:23 +0100 Subject: [PATCH] Add response runtime type check --- x-pack/plugins/infra/common/alerting/logs/types.ts | 4 ++-- .../lib/alerting/log_threshold/log_threshold_executor.ts | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/infra/common/alerting/logs/types.ts b/x-pack/plugins/infra/common/alerting/logs/types.ts index a47e9338c78fa5..37a8e1effd988a 100644 --- a/x-pack/plugins/infra/common/alerting/logs/types.ts +++ b/x-pack/plugins/infra/common/alerting/logs/types.ts @@ -121,7 +121,7 @@ export const LogDocumentCountAlertParamsRT = rt.intersection([ export type LogDocumentCountAlertParams = rt.TypeOf; -const UngroupedSearchQueryResponseRT = rt.intersection([ +export const UngroupedSearchQueryResponseRT = rt.intersection([ commonSearchSuccessResponseFieldsRT, rt.type({ hits: rt.type({ @@ -134,7 +134,7 @@ const UngroupedSearchQueryResponseRT = rt.intersection([ export type UngroupedSearchQueryResponse = rt.TypeOf; -const GroupedSearchQueryResponseRT = rt.intersection([ +export const GroupedSearchQueryResponseRT = rt.intersection([ commonSearchSuccessResponseFieldsRT, rt.type({ aggregations: rt.type({ diff --git a/x-pack/plugins/infra/server/lib/alerting/log_threshold/log_threshold_executor.ts b/x-pack/plugins/infra/server/lib/alerting/log_threshold/log_threshold_executor.ts index 93caaae1112722..c49fe4f6985f34 100644 --- a/x-pack/plugins/infra/server/lib/alerting/log_threshold/log_threshold_executor.ts +++ b/x-pack/plugins/infra/server/lib/alerting/log_threshold/log_threshold_executor.ts @@ -11,6 +11,8 @@ import { Comparator, LogDocumentCountAlertParams, Criterion, + GroupedSearchQueryResponseRT, + UngroupedSearchQueryResponseRT, UngroupedSearchQueryResponse, GroupedSearchQueryResponse, LogDocumentCountAlertParamsRT, @@ -324,7 +326,7 @@ const getUngroupedResults = async ( index: string, callCluster: AlertServices['callCluster'] ) => { - return await callCluster('search', query); + return decodeOrThrow(UngroupedSearchQueryResponseRT)(await callCluster('search', query)); }; const getGroupedResults = async ( @@ -338,9 +340,8 @@ const getGroupedResults = async ( while (true) { const queryWithAfterKey = { ...query }; queryWithAfterKey.body.aggregations.groups.composite.after = lastAfterKey; - const groupResponse: GroupedSearchQueryResponse = await callCluster( - 'search', - queryWithAfterKey + const groupResponse: GroupedSearchQueryResponse = decodeOrThrow(GroupedSearchQueryResponseRT)( + await callCluster('search', queryWithAfterKey) ); compositeGroupBuckets = [ ...compositeGroupBuckets,