Skip to content

Commit 1391c68

Browse files
authored
blog: post block-unreasonable-queries-with-infini-gateway (#17)
1 parent 9a32d19 commit 1391c68

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed
53.2 KB
Loading
152 KB
Loading
66.6 KB
Loading
57.7 KB
Loading
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
title: "Protect Elasticsearch with INFINI Gateway: Block Unreasonable Queries"
3+
meta_title: "Protect Elasticsearch with INFINI Gateway: Block Unreasonable Queries"
4+
description: "Protect Elasticsearch with INFINI Gateway: Block Unreasonable Queries."
5+
date: 2025-03-22T10:00:00+08:00
6+
image: "/images/posts/2025/block-unreasonable-queries-with-infini-gateway/cover.jpg"
7+
categories: ["Elasticsearch", "Gateway"]
8+
author: "Frank"
9+
tags: ["Elasticsearch", "Gateway", "Search"]
10+
lang: "en"
11+
category: "Technology"
12+
subcategory: "Gateway"
13+
draft: false
14+
---
15+
16+
This article explores how to use INFINI Gateway to block unreasonable queries from being sent to Elasticsearch. The same method also applies to Opensearch and INFINI Easysearch.
17+
18+
In past experiences dealing with Elasticsearch OOM (Out of Memory) issues, we found that many cases were caused by query operations leading to node OOM. After investigation, these cases mainly fall into two categories:
19+
20+
1. **Excessive Query Throughput**: When the frequency or complexity of query requests exceeds the cluster's processing capacity, it can cause node memory exhaustion, triggering OOM.
21+
2. **Unreasonable Queries**: Certain types of queries (e.g., deeply nested queries, deep pagination, or complex aggregations) may require significant memory resources, potentially causing OOM during execution.
22+
23+
By identifying and optimizing these query patterns, OOM incidents can be effectively reduced. For managing excessive query throughput, refer to previous articles. Below, we focus on blocking unreasonable queries to protect cluster stability.
24+
25+
## **Unreasonable Queries**
26+
Unreasonable queries are those that consume excessive system resources (e.g., CPU, Memory), are overly complex, take too long to execute, or require extensive computational resources. These queries can lead to high load, resource exhaustion, and negatively impact cluster stability, response speed, and user experience.
27+
28+
Examples of unreasonable queries include:
29+
30+
+ Nested aggregation queries
31+
+ Complex regex-based fuzzy matching
32+
+ Deep pagination queries (e.g., from: 10000, size: 10)
33+
+ Script queries
34+
+ Large-scale nested aggregations
35+
36+
To prevent these queries from affecting Elasticsearch clusters, INFINI Gateway can be used to block them.
37+
38+
## **Request Context**
39+
INFINI Gateway provides access to a wealth of information through the request context, which serves as the entry point to access details such as request source and body. The **_ctx** keyword is used as a prefix to access this context.
40+
![block-unreasonable-queries-with-infini-gateway](/images/posts/2025/block-unreasonable-queries-with-infini-gateway/1.png)
41+
42+
For more details on available context information, refer to the [documentation](https://docs.infinilabs.com/gateway/main/docs/references/context/).
43+
44+
## **Context Filter**
45+
Context Filter is an online filter provided by INFINI Gateway that allows traffic filtering based on request context. By defining a set of matching rules, traffic can be flexibly screened. The filter supports multiple matching modes, including:
46+
47+
+ Prefix matching
48+
+ Suffix matching
49+
+ Fuzzy matching
50+
+ Regex matching
51+
52+
For matched requests, they can be directly blocked (denied) with a custom response message. The key is to identify the critical keywords or characteristics of unreasonable requests.
53+
54+
## **Steps to Implement**
55+
1. **Identify Keywords**: Determine the key features or keywords in problematic query requests.
56+
2. **Configure Matching Rules**: Define matching rules in **context_filter**, selecting the appropriate matching mode (e.g., prefix, suffix, fuzzy, or regex).
57+
3. **Block Requests**: Once keywords are matched, INFINI Gateway will automatically block the request and return the specified message.
58+
59+
For more details, refer to the [documentation](https://docs.infinilabs.com/gateway/main/docs/references/filters/context_filter/).
60+
61+
## **Example: Blocking Wildcard Queries**
62+
Consider a wildcard query example:
63+
64+
```plain
65+
GET yf-test-1shard/_search
66+
{
67+
"query": {
68+
"wildcard": {
69+
"path.keyword": {
70+
"value": "/a*"
71+
}
72+
}
73+
}
74+
}
75+
```
76+
This query searches for documents where the **path** field starts with **/a**.
77+
![block-unreasonable-queries-with-infini-gateway](/images/posts/2025/block-unreasonable-queries-with-infini-gateway/2.png)
78+
79+
80+
**Step 1**: Identify the keyword as **wildcard":** to specifically target wildcard queries. For sometimes the query URL may contain the term expand_wildcards.
81+
82+
**Step 2**: Edit the INFINI Gateway configuration file to add a **context_filter** rule:
83+
84+
```yaml
85+
- name: default_flow
86+
filter:
87+
- context_filter:
88+
context: _ctx.request.to_string
89+
message: 'Request blocked. Reason: Forbidden. Please contact the administrator at 010-111111.'
90+
status: 403
91+
action: deny
92+
must_not:
93+
contain:
94+
- "wildcard\":"
95+
```
96+
97+
This rule blocks requests containing the keyword **wildcard":** and returns a custom message:"Request blocked. Reason: Forbidden. Please contact the administrator at 010–111111."
98+
99+
**Step 3**: Test to ensure wildcard queries are blocked.
100+
![block-unreasonable-queries-with-infini-gateway](/images/posts/2025/block-unreasonable-queries-with-infini-gateway/3.png)
101+
102+
INFINI Gateway successfully blocks wildcard queries and returns the defined message. This method prevents high-resource-consuming queries from being sent to the Elasticsearch cluster, avoiding performance issues.
103+

0 commit comments

Comments
 (0)