From 208b6b4d25995dc9e85ec40af7f9bfe590c4eef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B3=BD=E8=BD=A9?= Date: Wed, 9 Jun 2021 08:58:34 +0800 Subject: [PATCH] change: check metadata_schema with check_schema like the other schema (#4381) Signed-off-by: spacewander --- apisix/admin/plugin_metadata.lua | 26 ++++++++++++++++++-------- apisix/core/schema.lua | 2 ++ apisix/plugins/batch-requests.lua | 9 ++++----- apisix/plugins/example-plugin.lua | 3 +++ apisix/plugins/http-logger.lua | 5 ++++- apisix/plugins/skywalking.lua | 6 +++--- t/plugin/error-log-logger.t | 4 ++++ 7 files changed, 38 insertions(+), 17 deletions(-) diff --git a/apisix/admin/plugin_metadata.lua b/apisix/admin/plugin_metadata.lua index 0277f4660499..3245fcc747db 100644 --- a/apisix/admin/plugin_metadata.lua +++ b/apisix/admin/plugin_metadata.lua @@ -20,6 +20,7 @@ local require = require local core = require("apisix.core") local api_router = require("apisix.api_router") +local injected_mark = "injected metadata_schema" local _M = { } @@ -50,15 +51,14 @@ local function check_conf(plugin_name, conf) return nil, {error_msg = "missing configurations"} end - local schema = plugin_object.metadata_schema or { - type = "object", - properties = {}, - } - if not schema.properties then - schema.properties = { - additionalProperties = false, + if not plugin_object.metadata_schema then + plugin_object.metadata_schema = { + type = "object", + ['$comment'] = injected_mark, + properties = {}, } end + local schema = plugin_object.metadata_schema -- inject interceptors schema to each plugins if schema.properties.interceptors @@ -70,7 +70,17 @@ local function check_conf(plugin_name, conf) core.log.info("schema: ", core.json.delay_encode(schema)) core.log.info("conf : ", core.json.delay_encode(conf)) - local ok, err = core.schema.check(schema, conf) + + local ok, err + if schema['$comment'] == injected_mark + -- check_schema is not required. If missing, fallback to check schema directly + or not plugin_object.check_schema + then + ok, err = core.schema.check(schema, conf) + else + ok, err = plugin_object.check_schema(conf, core.schema.TYPE_METADATA) + end + if not ok then return nil, {error_msg = "invalid configuration: " .. err} end diff --git a/apisix/core/schema.lua b/apisix/core/schema.lua index 7256f08c3ca3..8f79e966f407 100644 --- a/apisix/core/schema.lua +++ b/apisix/core/schema.lua @@ -22,7 +22,9 @@ local pcall = pcall local _M = { version = 0.3, + TYPE_CONSUMER = 1, + TYPE_METADATA = 2, } diff --git a/apisix/plugins/batch-requests.lua b/apisix/plugins/batch-requests.lua index 6b7f14eacab8..559bd8e5b347 100644 --- a/apisix/plugins/batch-requests.lua +++ b/apisix/plugins/batch-requests.lua @@ -111,12 +111,11 @@ local _M = { } -function _M.check_schema(conf) - local ok, err = core.schema.check(schema, conf) - if not ok then - return false, err +function _M.check_schema(conf, schema_type) + if schema_type == core.schema.TYPE_METADATA then + return core.schema.check(metadata_schema, conf) end - return true + return core.schema.check(schema, conf) end diff --git a/apisix/plugins/example-plugin.lua b/apisix/plugins/example-plugin.lua index abe149e7d87d..042779fa9916 100644 --- a/apisix/plugins/example-plugin.lua +++ b/apisix/plugins/example-plugin.lua @@ -53,6 +53,9 @@ local _M = { function _M.check_schema(conf, schema_type) + if schema_type == core.schema.TYPE_METADATA then + return core.schema.check(metadata_schema, conf) + end return core.schema.check(schema, conf) end diff --git a/apisix/plugins/http-logger.lua b/apisix/plugins/http-logger.lua index 34370462d4b5..0ca37e7090bd 100644 --- a/apisix/plugins/http-logger.lua +++ b/apisix/plugins/http-logger.lua @@ -81,7 +81,10 @@ local _M = { } -function _M.check_schema(conf) +function _M.check_schema(conf, schema_type) + if schema_type == core.schema.TYPE_METADATA then + return core.schema.check(metadata_schema, conf) + end return core.schema.check(schema, conf) end diff --git a/apisix/plugins/skywalking.lua b/apisix/plugins/skywalking.lua index 2cf99c30ae47..ec5b190410b8 100644 --- a/apisix/plugins/skywalking.lua +++ b/apisix/plugins/skywalking.lua @@ -23,7 +23,7 @@ local math = math local require = require local plugin_name = "skywalking" -local metadata_schema = { +local attr_schema = { type = "object", properties = { service_name = { @@ -66,7 +66,7 @@ local _M = { priority = -1100, -- last running plugin, but before serverless post func name = plugin_name, schema = schema, - metadata_schema = metadata_schema, + attr_schema = attr_schema, } @@ -117,7 +117,7 @@ function _M.init() "plugin_attr", plugin_name) or {} local_plugin_info = core.table.clone(local_plugin_info) - local ok, err = core.schema.check(metadata_schema, local_plugin_info) + local ok, err = core.schema.check(attr_schema, local_plugin_info) if not ok then core.log.error("failed to check the plugin_attr[", plugin_name, "]", ": ", err) diff --git a/t/plugin/error-log-logger.t b/t/plugin/error-log-logger.t index 7c139a16722f..0fc9f2cd6273 100644 --- a/t/plugin/error-log-logger.t +++ b/t/plugin/error-log-logger.t @@ -126,11 +126,15 @@ plugins: }]] ) + -- ensure the request is rejected even this plugin doesn't + -- have check_schema method + ngx.status = code core.log.warn("this is a warning message for test.") } } --- request GET /tg +--- error_code: 400 --- response_body --- error_log eval qr/please set the correct plugin_metadata for error-log-logger/