Skip to content
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

fix: always inject disable field into plugin schema #3272

Merged
merged 2 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions apisix/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,27 @@ local function load_plugin(name, plugins_list, is_stream_plugin)
return
end

if plugin.schema and plugin.schema.type == "object" then
if not plugin.schema.properties or
core.table.nkeys(plugin.schema.properties) == 0
then
plugin.schema.properties = core.schema.plugin_disable_schema
if type(plugin.schema) ~= "table" then
core.log.error("invalid plugin [", name, "] schema field")
return
end

if not plugin.schema.properties then
plugin.schema.properties = {}
end

local properties = plugin.schema.properties
local plugin_injected_schema = core.schema.plugin_injected_schema

if properties['$comment'] ~= plugin_injected_schema['$comment'] then
if properties.disable then
core.log.error("invalid plugin [", name,
"]: found forbidden 'disable' field in the schema")
return
end

properties.disable = plugin_injected_schema.disable
properties['$comment'] = plugin_injected_schema['$comment']
end

plugin.name = name
Expand Down
4 changes: 4 additions & 0 deletions apisix/plugins/error-log-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ local metadata_schema = {
},
required = {"host", "port"}
}
local schema = {
type = "object",
}


local log_level = {
Expand All @@ -77,6 +80,7 @@ local _M = {
version = 0.1,
priority = 1091,
name = plugin_name,
schema = schema,
metadata_schema = metadata_schema,
}

Expand Down
7 changes: 5 additions & 2 deletions apisix/schema_def.lua
Original file line number Diff line number Diff line change
Expand Up @@ -739,8 +739,11 @@ _M.plugins = {
_M.id_schema = id_schema


_M.plugin_disable_schema = {
disable = {type = "boolean"}
_M.plugin_injected_schema = {
["$comment"] = "this is a mark for our injected plugin schema",
disable = {
type = "boolean",
}
}


Expand Down
2 changes: 1 addition & 1 deletion t/admin/plugins.t
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ plugins:
}
}
--- response_body eval
qr/\{"metadata_schema":\{"additionalProperties":false,"properties":\{"ikey":\{"minimum":0,"type":"number"\},"skey":\{"type":"string"\}\},"required":\["ikey","skey"\],"type":"object"\},"priority":0,"schema":\{"properties":\{"i":\{"minimum":0,"type":"number"\},"ip":\{"type":"string"\},"port":\{"type":"integer"\},"s":\{"type":"string"\},"t":\{"minItems":1,"type":"array"\}\},"required":\["i"\],"type":"object"\},"version":0.1\}/
qr/\{"metadata_schema":\{"additionalProperties":false,"properties":\{"ikey":\{"minimum":0,"type":"number"\},"skey":\{"type":"string"\}\},"required":\["ikey","skey"\],"type":"object"\},"priority":0,"schema":\{"properties":\{"\$comment":"this is a mark for our injected plugin schema","disable":\{"type":"boolean"\},"i":\{"minimum":0,"type":"number"\},"ip":\{"type":"string"\},"port":\{"type":"integer"\},"s":\{"type":"string"\},"t":\{"minItems":1,"type":"array"\}\},"required":\["i"\],"type":"object"\},"version":0.1\}/
--- no_error_log
[error]

Expand Down
16 changes: 13 additions & 3 deletions t/control/schema.t
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,26 @@ __DATA__
"upstream": {"type":"object"},
"upstream_hash_header_schema": {"type":"string"},
"upstream_hash_vars_schema": {"type":"string"},
},
},]] .. [[
"plugins": {
"example-plugin": {
"version": 0.1,
"priority": 0,
"schema": {"type":"object"},
"schema": {
"type":"object",
"properties": {
"disable": {"type": "boolean"}
}
},
"metadata_schema": {"type":"object"}
},
"mqtt-proxy": {
"schema": {"type":"object"},
"schema": {
"type":"object",
"properties": {
"disable": {"type": "boolean"}
}
},
"priority": 1000
},
"basic-auth": {
Expand Down