-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(plugin): add graphql-limit-count plugin #13372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlinsRan
wants to merge
11
commits into
master
Choose a base branch
from
feat/graphql-limit-count
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3611ddb
feat(plugin): add graphql-limit-count plugin
AlinsRan 2618714
fix: add license header, update config.yaml.example and docs format
AlinsRan aa1a1d0
docs: rewrite graphql-limit-count docs to follow open-source format
AlinsRan c49acd3
fix: address review comments for graphql-limit-count plugin
AlinsRan 3019135
fix: fix typo unparseable -> unparsable in test description
AlinsRan ef7e21d
fix: fix luacheck warning - avoid overwriting err variable
AlinsRan ce2f301
refactor: rename max_query_depth to node_depth
AlinsRan a400d7c
fix: remove manual http_config shared dict declarations from test
AlinsRan d8ef31e
fix: add plugin-graphql-limit-count shared dicts to test framework
AlinsRan a2506b4
fix(tests): use pipelined_requests for rate limit quota exhaustion test
AlinsRan 34fafe7
fix(tests): reindex test cases after merging TEST 2+3
AlinsRan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| -- | ||
| -- Licensed to the Apache Software Foundation (ASF) under one or more | ||
| -- contributor license agreements. See the NOTICE file distributed with | ||
| -- this work for additional information regarding copyright ownership. | ||
| -- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| -- (the "License"); you may not use this file except in compliance with | ||
| -- the License. You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, software | ||
| -- distributed under the License is distributed on an "AS IS" BASIS, | ||
| -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| -- See the License for the specific language governing permissions and | ||
| -- limitations under the License. | ||
| -- | ||
| local limit_count = require("apisix.plugins.limit-count.init") | ||
| local core = require("apisix.core") | ||
| local gq_parse = require("graphql").parse | ||
| local limit_count_ver = require("resty.limit.count")._VERSION | ||
|
|
||
| local type = type | ||
| local pairs = pairs | ||
| local pcall = pcall | ||
|
|
||
| local plugin_name = "graphql-limit-count" | ||
| local _M = { | ||
| version = 0.1, | ||
| priority = 1004, | ||
| name = plugin_name, | ||
| schema = limit_count.schema, | ||
| } | ||
|
|
||
|
|
||
| function _M.check_schema(conf) | ||
| return limit_count.check_schema(conf) | ||
| end | ||
|
|
||
|
|
||
| local GRAPHQL_REQ_QUERY = "query" | ||
| local GRAPHQL_REQ_MIME_JSON = "application/json" | ||
| local GRAPHQL_REQ_MIME_GQL = "application/graphql" | ||
|
|
||
|
|
||
| local fetch_graphql_body = { | ||
| ["POST"] = function(ctx, max_size) | ||
| local body, err = core.request.get_body(max_size, ctx) | ||
| if not body then | ||
| return nil, "failed to read graphql data, " .. (err or "request body has zero size") | ||
| end | ||
|
|
||
| return body | ||
| end | ||
| } | ||
|
|
||
|
|
||
| local check_graphql_request = { | ||
| ["POST"] = function(ctx, body) | ||
| local content_type = core.request.header(ctx, "Content-Type") | ||
| if content_type == GRAPHQL_REQ_MIME_JSON then | ||
|
AlinsRan marked this conversation as resolved.
Outdated
|
||
| local res, err = core.json.decode(body) | ||
| if not res then | ||
| return false, "invalid graphql request, " .. err | ||
| end | ||
|
|
||
| if not res[GRAPHQL_REQ_QUERY] then | ||
| return false, "invalid graphql request, json body[" .. | ||
| GRAPHQL_REQ_QUERY .. "] is nil" | ||
| end | ||
|
|
||
| return true, res[GRAPHQL_REQ_QUERY] | ||
| end | ||
|
|
||
| if content_type == GRAPHQL_REQ_MIME_GQL then | ||
|
AlinsRan marked this conversation as resolved.
Outdated
|
||
| if not core.string.find(body, GRAPHQL_REQ_QUERY) then | ||
| return false, "invalid graphql request, can't find '" .. | ||
| GRAPHQL_REQ_QUERY .. "' in request body" | ||
| end | ||
|
AlinsRan marked this conversation as resolved.
Outdated
|
||
| return true, body | ||
|
AlinsRan marked this conversation as resolved.
Outdated
|
||
| end | ||
|
|
||
| return false, "invalid graphql request, error content-type: " .. (content_type or "") | ||
| end | ||
| } | ||
|
|
||
|
|
||
| -- Finds the depth of the graphql query from the given AST table. | ||
| local function node_depth(t) | ||
| local depth = 0 | ||
| if type(t) ~= "table" then | ||
| return depth | ||
| end | ||
|
|
||
| for k, v in pairs(t) do | ||
| if k == "selections" then | ||
| depth = depth + 1 | ||
| end | ||
| depth = depth + node_depth(v) | ||
| end | ||
|
|
||
| return depth | ||
|
AlinsRan marked this conversation as resolved.
Outdated
|
||
| end | ||
|
|
||
|
|
||
| function _M.access(conf, ctx) | ||
| if limit_count_ver < '1.0.0' then | ||
| core.log.error("need to build APISIX-Base to support GraphQL limit count") | ||
| return 501 | ||
| end | ||
|
|
||
| local method = core.request.get_method() | ||
| if method ~= "POST" then | ||
| return 405 | ||
| end | ||
|
|
||
| local body, err = fetch_graphql_body[method](ctx) | ||
|
AlinsRan marked this conversation as resolved.
Outdated
|
||
| if not body then | ||
| core.log.error(err) | ||
| return 400, {message = "Invalid graphql request: cant't get graphql request body"} | ||
|
AlinsRan marked this conversation as resolved.
Outdated
|
||
| end | ||
|
|
||
| local is_graphql_req, query_or_err = check_graphql_request[method](ctx, body) | ||
| if not is_graphql_req then | ||
| core.log.error(query_or_err) | ||
| return 400, {message = "Invalid graphql request: no query"} | ||
|
AlinsRan marked this conversation as resolved.
Outdated
|
||
| end | ||
|
|
||
| local ok, res = pcall(gq_parse, query_or_err) | ||
| if not ok then | ||
| core.log.error("failed to parse graphql: ", res, ", body: ", body) | ||
| return 400, {message = "Invalid graphql request: failed to parse graphql query"} | ||
| end | ||
|
|
||
| local n = #res.definitions | ||
| if n == 0 then | ||
| core.log.error("failed to parse graphql: empty query, body: ", body) | ||
| return 400, {message = "Invalid graphql request: empty graphql query"} | ||
| end | ||
|
|
||
| local depth = node_depth(res) | ||
| core.log.info("graphql node depth: ", depth) | ||
|
|
||
| return limit_count.rate_limit(conf, ctx, plugin_name, depth) | ||
|
AlinsRan marked this conversation as resolved.
|
||
| end | ||
|
|
||
|
|
||
| return _M | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.