Skip to content

Commit f48956d

Browse files
committed
CMR-4781: Temporarily block a very specific query (#471)
* CMR-4781: Temporarily block a very specific query * CMR-4781: Control whether we block queries via a configuration parameter. Defaults to true
1 parent 8d5f9bf commit f48956d

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

common-lib/src/cmr/common/api/errors.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
:conflict 409
1616
:invalid-content-type 415
1717
:invalid-data 422
18+
:too-many-requests 429
1819
:service-unavailable 503})
1920

2021
(def CONTENT_TYPE_HEADER "Content-Type")

search-app/src/cmr/search/api/concepts_search.clj

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[cmr.common-app.api.routes :as common-routes]
66
[cmr.common-app.services.search :as search]
77
[cmr.common.cache :as cache]
8+
[cmr.common.config :refer [defconfig]]
89
[cmr.common.log :refer (debug info warn error)]
910
[cmr.common.mime-types :as mt]
1011
[cmr.common.services.errors :as svc-errors]
@@ -47,6 +48,28 @@
4748
results (query-svc/find-concepts-by-json-query ctx concept-type params json-query)]
4849
(core-api/search-response ctx results)))
4950

51+
(defconfig block-queries
52+
"Indicates whether we are going to block a specific excessive query."
53+
{:type Boolean
54+
:default true})
55+
56+
(defn- block-excessive-queries
57+
"Temporary solution to prevent a specific query from overloading the CMR search resources."
58+
[ctx concept-type result-format params]
59+
(when (and (block-queries)
60+
(= concept-type :granule)
61+
(= :json result-format)
62+
(= "MCD43A4" (:short_name params))
63+
(contains? params ""))
64+
(warn (format "Blocking %s query from client %s in format %s with params %s."
65+
(name concept-type)
66+
(:client-id ctx)
67+
(rfh/printable-result-format result-format)
68+
(pr-str params)))
69+
(svc-errors/throw-service-error
70+
:too-many-requests
71+
"Excessive query rate. Please contact [email protected].")))
72+
5073
(defn- find-concepts-by-parameters
5174
"Invokes query service to parse the parameters query, find results, and
5275
return the response"
@@ -59,14 +82,15 @@
5982
ctx (assoc ctx :query-string body :scroll-id scroll-id)
6083
params (core-api/process-params concept-type params path-w-extension headers mt/xml)
6184
result-format (:result-format params)
85+
_ (block-excessive-queries ctx concept-type result-format params)
6286
_ (info (format "Searching for %ss from client %s in format %s with params %s."
6387
(name concept-type) (:client-id ctx)
6488
(rfh/printable-result-format result-format) (pr-str params)))
6589
search-params (if cached-search-params
6690
cached-search-params
6791
(lp/process-legacy-psa params))
6892
results (query-svc/find-concepts-by-parameters ctx concept-type search-params)]
69-
(if (:scroll-id results)
93+
(if (:scroll-id results)
7094
(core-api/search-response ctx results search-params)
7195
(core-api/search-response ctx results))))
7296

system-int-test/test/cmr/system_int_test/search/granule_search_test.clj

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,11 @@
369369
;; exclude granules by echo_granule_id or concept_id (including parent concept_id) params
370370
(deftest exclude-granules-by-echo-granule-n-concept-ids
371371
(let [coll1 (d/ingest-umm-spec-collection "PROV1" (data-umm-c/collection {:EntryTitle "E1"
372-
:ShortName "S1"
373-
:Version "V1"}))
372+
:ShortName "S1"
373+
:Version "V1"}))
374374
coll2 (d/ingest-umm-spec-collection "PROV2" (data-umm-c/collection {:EntryTitle "E2"
375-
:ShortName "S2"
376-
:Version "V2"}))
375+
:ShortName "S2"
376+
:Version "V2"}))
377377
coll1-cid (get-in coll1 [:concept-id])
378378
coll2-cid (get-in coll2 [:concept-id])
379379
gran1 (d/ingest "PROV1" (dg/granule-with-umm-spec-collection coll1 coll1-cid {:cloud-cover 0.8}))
@@ -414,11 +414,11 @@
414414
;; Find granules by echo_granule_id, echo_collection_id and concept_id params
415415
(deftest search-by-concept-id
416416
(let [coll1 (d/ingest-umm-spec-collection "PROV1" (data-umm-c/collection {:EntryTitle "E1"
417-
:ShortName "S1"
418-
:Version "V1"}))
417+
:ShortName "S1"
418+
:Version "V1"}))
419419
coll2 (d/ingest-umm-spec-collection "PROV2" (data-umm-c/collection {:EntryTitle "E2"
420-
:ShortName "S2"
421-
:Version "V2"}))
420+
:ShortName "S2"
421+
:Version "V2"}))
422422
coll1-cid (get-in coll1 [:concept-id])
423423
coll2-cid (get-in coll2 [:concept-id])
424424
gran1 (d/ingest "PROV1" (dg/granule-with-umm-spec-collection coll1 coll1-cid))
@@ -534,3 +534,9 @@
534534
(is (= {:status 400
535535
:errors [(smsg/mixed-arity-parameter-msg :concept-id)]}
536536
(search/make-raw-search-query :granule ".json?concept_id=G&concept_id[pattern]=true"))))))
537+
538+
(deftest block-excessive-queries-test
539+
(testing "Blocking those MCD43A4 queries"
540+
(is (= {:status 429
541+
:errors ["Excessive query rate. Please contact [email protected]."]}
542+
(search/make-raw-search-query :granule ".json?short_name=MCD43A4&&page_size=5")))))

0 commit comments

Comments
 (0)